Creating Spatial Data

Creating Spatial Data

GIS 5222

Jake K. Carr

Week 8

Creating Spatial Data

Jake K. Carr

Dealing with Flat Files

Sometimes the data (geographic and otherwise) we get is in a `flat' file format.

We mostly see `flat' files that come in .txt format, but .csv are `just text` files that have a comma separating values in a `row'. See Coordinates.csv.

These `separating' characters are called delimiters and there are many choices to use, such as: tabs, commas, spaces, pipes.

It is important to remember how your data is delimited when reading and writing text based data to Python.

Creating Spatial Data

Jake K. Carr

The Open Function

We can `open' files in Python with the open function.

A better way to think of it to say that Python is `reading' a text file into memory so that we can then perform some type of Python-based action onto it.

>>> myFile = open(r"C:\Users\carr .526\ Google Drive\GEOG 5222\ Lectures\Lecture 8\Data\Coordinates.csv",'r')

Here I use the argument `r' after the file path and name to tell Python that I only want to `read' the file.

Creating Spatial Data

Jake K. Carr

The readline Method

Once I have `opened' a file I can the `operate' on it using file specific methods.

The readline method does just that, it reads a line (and only one line).

>>> stHead = myFile.readline() >>> stHead 'Observation ,Name , Lat , Lon\n'

Here I create an object called stHead to which I assign the first line of the `opened' file.

Creating Spatial Data

Jake K. Carr

The readline Method

Note that a line in a flat file is the same thing as a row in an attribute table.

What is in stHead?

It's the variable names for all of the columns/fields in Coordinates.csv.

Note that stHead is a string (how do I know that?), therefore I can use some of those string manipulation methods we learned back in Lab 1.

Creating Spatial Data

Jake K. Carr

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download