GeoPandas Primer

Reading, writing, plotting, projections

Start by importing Pandas, GeoPandas, and matplotlib.pyplot

Type:
>>> import pandas as pd
>>> import geopandas as gpd
>>> import matplotlib.pyplot as plt
>>> import os

In [2]:
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import os

Change your working directory to the home folder

Type the following to move back a directory:
>>> os.chdir('../../')

In [3]:
os.chdir('../..')

Let's get some data, anything will do...

Type:
>>> gpd.read_file('workshopdata/WAML.geojson')

In [3]:
gpd.read_file('workshopdata/WAML.geojson')
Out[3]:
Thing Exec geometry
0 T Bruce (POLYGON ((-119.9271766 39.1936225, -119.92727...
1 W Susan (POLYGON ((-120.1474632551673 39.3841351669116...
2 A Louise (POLYGON ((-119.9697264965678 39.2644361944219...
3 M Tom (POLYGON ((-119.852629337961 39.27253041522475...
4 L Matt (POLYGON ((-119.7250770759073 39.3582716674715...

You imported a geojson file named WAML! This is referred to as a GeoDataFrame.

Now make it a variable by typing:
>>> waml = gpd.read_file('workshopdata/WAML.geojson')

In [4]:
waml = gpd.read_file('workshopdata/WAML.geojson')

Nice!

What the heck is this thing?

Plot it with matplotlib by typing:
>>> waml.plot()

In [5]:
waml.plot()
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f78f4d7a7f0>

Amazing.

Let's now find out the coordinate reference system of WAML.geojson

Type:
>>> waml.crs

In [6]:
waml.crs
Out[6]:
{'init': 'epsg:4326'}

Good. The CRS is epsg 4326... AKA WGS84

Let's create a copy of the layer and reset it's CRS to Lambert Conformal Conic

Type:
>>> waml_lcc=waml.to_crs(epsg=2163)

In [7]:
waml_lcc = waml.to_crs(epsg=2163)

Now, let's view the difference

Type:
>>> waml_lcc.plot()

In [8]:
waml_lcc.plot()
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f78f2d17390>

Okay, there are other methods to reset the crs, too. Let's change it to Transverse Mercator

Type:
>>> waml_tm = waml.to_crs({'proj':'tmerc'})

In [9]:
waml_tm = waml.to_crs({'proj':'tmerc'})

view the difference

Type:
>>> waml_tm.plot()

In [10]:
waml_tm.plot()
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f78f286fe10>

Let's change it to NAD83 Nevada Central?

How do I know what code to use? Pro tip: https://spatialreference.org

Type:
>>> waml_Nev = waml.to_crs(epsg = 3422)

In [11]:
waml_Nev = waml.to_crs(epsg = 3422)

Plot it:

Type:
>>> waml_Nev.plot()

In [12]:
waml_Nev.plot()
Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f78f2812c18>

Okay, so far we've basically been creating all this stuff using RAM. Let's export the waml_Nev variable to a shapefile.

Type:
>>> waml_Nev.to_file('waml_Nev.shp')

In [13]:
waml_Nev.to_file('waml_Nev.shp')

Go check your data folder to confirm.

But shapefiles are lame and boring. Let's make a geojson file instead:

Type:
>>> waml_Nev.to_file('waml_Nev.geojson', driver='GeoJSON')

In [14]:
waml_Nev.to_file('waml_Nev.geojson', driver = 'GeoJSON')

Nice work!