Calico Python GIS

From IPRE Wiki
Jump to: navigation, search

This page details using Calico Python as a Geographic Information System (GIS).

For these experiments you will need Calico 1.0.4 or greater, or 1.0.1 and these files:

  1. capitals.dat
  2. states.dat
  3. gis.py

The first two are data files in a made-up format. The third is a Geographic Information System (GIS) functions in Calico Python.

You can run the gis.py program in Calico by opening it in the editor, and running it. Interact with the GIS window:

GIS.gif

This simple example has two simple actions:

  • click on the sides to zoom in/out
  • click on the map to center on clicked location

But you might also be interested in displaying your own data.

You can find latitude, longitude data in many places. For example, here is the lat/long of the hurricane Irene:

http://www.weather.gov/view/national.php?prodtype=hurricane

You can look through that text and make a table of the data, like so:

23:00, 37.3, 75.4, 20, 16, 80
00:00, 37.3, 75.4, 20, 16, 80
01:00, 37.3, 75.4, 20, 16, 80
02:00, 38.1, 75.0, 20, 17, 80
03:00, 38.1, 75.0, 20, 17, 80
05:00, 39.2, 74.5, 20, 18, 75
09:00, 40.3, 74.1, 20, 25, 75
10:00, 40.3, 74.1, 20, 25, 75
11:00, 41.4, 73.7, 20, 26, 60
12:00, 41.4, 73.7, 20, 26, 60
13:00, 41.4, 73.7, 20, 26, 60
14:00, 42.2, 73.2, 20, 26, 60
15:00, 42.2, 73.2, 20, 26, 60
17:00, 42.7, 72.8, 20, 26, 50
18:00, 42.7, 72.8, 20, 26, 50
20:00, 44.1, 72.1, 25, 26, 50

Here, we list the time, latitude, longitude, direction (degrees), movement speed, and wind speed. Save the file in the same directory as gis.py, and name it "irene.dat".

You can read the file in with code like the following:

fp = open("irene.dat")
dots = []
for line in fp:
    print(line)
    data = line.strip().split(",")
    x, y = (float(data[1]), float(data[2]))
    dots.append((x, y))

Here is a function using the function ll2xy() from the gis.py:

def drawIrene(win):
    line = Line()
    for (x,y) in dots:
        x, y = gis.ll2xy(x, y)
        c = Circle(Point(x, y), 10)
        line.append(Point(x,y))
        c.fill = Color(255, 255, 0, 62)
        c.draw(win)
    line.draw(win)
    line.border = 5

If you insert calls to drawIrene(win) in the gis.py program, you can see the path of the storm:

Irene.gif

Some ideas for enhancement:

  • get all of the data of the path the hurricane travelled
  • display additional data, such as direction, movement speed, or wind speed
  • make the window display the time of the hurricane if you mouse over it

There are also sites that show predictions:

Try plotting that data as well.

Plotting Data By State

Another use of the state polygons is to color each one a different color based on some criteria.

For example, here is some data by state:

http://www.census.gov/compendia/statab/cats/health_nutrition/health_risk_factors.html

  1. Save the xls file
  2. Open it in your favorite spreadsheet program
  3. Select the data, and save it as csv
  4. You may need to open the file to remove unwanted portions
  5. You end up with something that looks like:
"Alabama","AL","01",22.2,25.2,19.4
"Alaska","AK","02",21.7,23.9,19.3
"Arizona","AZ","04",15.9,18.2,13.7
"Arkansas","AR","05",22.4,24.4,20.4
"California","CA","06",14.0,17.8,10.3
"Colorado","CO","08",17.6,19.8,15.5
"Connecticut","CT","09",16.0,17.3,14.8
"Delaware","DE","10",17.8,20.4,15.4
"District of Columbia","DC","11",16.4,19.4,13.8
"Florida","FL","12",17.5,18.7,16.4
"Georgia","GA","13",19.5,21.7,17.4
"Hawaii","HI","15",15.4,18.2,12.7
"Idaho","ID","16",16.9,18.4,15.4
"Illinois","IL","17",21.3,25.4,17.5
"Indiana","IN","18",26.1,28.5,23.9
"Iowa","IA","19",18.8,21.0,16.7
"Kansas","KS","20",17.9,19.8,16.1
"Kentucky","KY","21",25.3,26.3,24.3
"Louisiana","LA","22",20.5,23.4,17.7
"Maine","ME","23",18.2,21.6,15.0
"Maryland","MD","24",14.9,16.1,13.9
"Massachusetts","MA","25",16.1,16.9,15.4
"Michigan","MI","26",20.4,22.5,18.4
"Minnesota","MN","27",17.6,19.3,15.8
"Mississippi","MS","28",22.7,25.4,20.3
"Missouri","MO","29",25.0,27.3,22.9
"Montana","MT","30",18.5,18.7,18.4
"Nebraska","NE","31",18.4,20.1,16.8
"Nevada","NV","32",22.3,24.5,20.0
"New Hampshire","NH","33",17.0,18.1,16.0
"New Jersey","NJ","34",14.8,17.4,12.4
"New Mexico","NM","35",19.4,22.0,16.9
"New York","NY","36",16.8,17.9,15.8
"North Carolina","NC","37",20.9,23.7,18.3
"North Dakota","ND","38",18.2,20.4,15.9
"Ohio","OH","39",20.2,21.5,19.0
"Oklahoma","OK","40",24.8,26.5,23.1
"Oregon","OR","41",16.3,17.4,15.3
"Pennsylvania","PA","42",21.4,23.4,19.6
"Rhode Island","RI","44",17.4,17.9,16.9
"South Carolina","SC","45",20.1,21.6,18.7
"South Dakota","SD","46",17.6,19.0,16.2
"Tennessee","TN","47",23.2,26.7,20.0
"Texas","TX","48",18.6,22.5,14.9
"Utah","UT","49",9.2,10.6,7.9
"Vermont","VT","50",16.8,18.4,15.2
"Virginia","VA","51",16.5,17.1,15.9
"Washington","WA","53",15.7,17.0,14.4
"West Virginia","WV","54",26.6,26.1,27.1
"Wisconsin","WI","55",19.9,21.7,18.2
"Wyoming","WY","56",19.4,20.0,18.9

In this example, we see "Current Cigarette Smoking by Sex and State: 2008" and the columns are: state name, abbreviation, state code, total percent of adults you smoke, male percent, and female percent. See the above URL for more details.

I saved the data in a file named "state-smoke.dat".

A bit of Calico Python code to read in the data and plot it:

from Graphics import *
import gis

data = {}
fp = open("state-smoke.dat")
for line in fp:
    line = line.strip()
    if line:
        state, abbrev, code, percent, male, female = line.split(",")
        data[abbrev[1:-1]] = state[1:-1], code[1:-1], float(percent), float(male), float(female)

values = []
for key in data:
    state, code, percent, male, female = data[key]
    if key in gis.states:
        values.append(percent)

minp = min(values)
maxp = max(values)

win = Window("Smokers by State", gis.width, gis.height)

for key in data:
    state, code, percent, male, female = data[key]
    if key in gis.states:
        color = 255 - ((percent - minp) / (maxp - minp)) * 255
        gis.drawState(win, key, Color(0, color, color))

Results:

State-smoke.jpg