GIS Logo GSP 118 (318): GIS Programming

Data Conversions

In Review

1. Introductions

Strings of text, like files from a spreadsheet, can contain a variety of data including names, coordinates, dates, and a huge variety of measurements. The problem is we need to "parse" the string to get at the data we are interested in and then "convert" the values we want from strings to the appropriate data type. Values may not be in the correct units we need to we may need to convert them from one unit to another. Below are some examples of different units with examples:

  • Peoples names
    • FirstName LastName: John Doe
    • LastName, FirstName: Doe, John
  • Dates
    • United States: MM/DD/YYYY: 12/31/2011
    • European: DD/MM/YYYY: 31/12/2011
    • Database: 2011-12-31
  • Coordinates:
    • Decimal Degrees: 40.123, -105.321
    • Degrees Minutes Seconds: 40° 21' 32" E 105° 30' 41" W
    • Decimal Minutes: 40° 23.213', -105° 40.21'

We may also need to convert data from one spatial reference system to another but this requires special software (like ArcGIS) and we'll talk more about it later.

For units, we may need to convert between various; linear measurements (meters, feet, kilometers, miles, etc.), area units (acres, hectares, square miles, etc.), volume measurements (quarts, gallons, liters, etc.). and other types of measurement.

You now know enough about Python to be able to make all the conversions listed above. You'll want to use "find()" to find the characters that separate each element in the strings and then use list indexing to "substring" the elements. You can also use "split()" if the characters that delimit each element are the same. Then you can format output strings as you desire.

2. Converting Strings to Numbers

Just as we have had to convert numbers to strings we also need to convert strings to numbers. The the float() function will convert a string to a floating-point value and the int() function will convert a string to an integer value. Try the code below.

x="12"
print(x+","+format(type(x)))
y=int(x)
print(format(y)+","+format(type(y)))

Note that the x value looks exactly like the y value when printed. This can cause you to think that you have a numeric value when you really have a string! Try the code below.

x="1"
y="2"
z=x+y
print(z)

The result that prints is 12! What's happening is pretty obvious with the code above, the x variable has a string with "1" in it and the y value has a string with "2" in it and we are concatenating the strings instead of adding two numbers. In the near future, you'll call a function that returns strings and you'll think the function returns numbers. Then you'll try to perform math on the values and you'll start to believe your computer has lost it's ability to do math. Stop and check the type of each variable to make sure you have the correct type and then convert the values to the correct type if needed.

Additional Resources

Python Documentation: String functions

Python Documentation: Defining Functions