Plotting with
GeoJSON
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Mary van Valkenburg
Data Science Program Manager,
Nashville So ware School
Neighborhoods GeoJSON
{ "type": "FeatureCollection",
"features": [
{ "type":"Feature",
"properties":{
"name":"Historic Buena Vista"
},"geometry":{
"type":"MultiPolygon","coordinates":[[[[-86.79511056795417,36.17575....
neighborhoods = gpd.read_file('./data/neighborhood_boundaries.geojson')
[Link](1)
name geometry
Historic Buena Vista (POLYGON ((-86.79511056795417 36.17575964963348...)))
VISUALIZING GEOSPATIAL DATA IN PYTHON
Geopandas dependencies
Fiona
provides an python API for OGR
GDAL/OGR
GDAL for translating raster data
OGR for translating vector data
VISUALIZING GEOSPATIAL DATA IN PYTHON
Comparing raster and vector graphics
raster image of Corfu vector image of Corfu
VISUALIZING GEOSPATIAL DATA IN PYTHON
Colormaps
VISUALIZING GEOSPATIAL DATA IN PYTHON
Plotting with color
school_districts.head(3)
first_name last_name position district geometry
Sharon Gentry Member 1 (POLYGON ((-86.771 36.383)...))
Jill Speering Vice-Chair 3 (POLYGON ((-86.753 36.404)...))
Jo Ann Brannon Member 2 (POLYGON ((-86.766 36.083)...))
VISUALIZING GEOSPATIAL DATA IN PYTHON
council_dists.plot( leg_kwds={'title':'District Number',
column='district', 'loc': 'upper left',
cmap='Set3', 'bbox_to_anchor':(1, 1.03),
legend=True) 'ncol':3}
[Link]('Council Districts') council_dists.plot(column='district',
[Link](); cmap='Set3',
legend=True,
legend_kwds=leg_kwds)
[Link]('Council Districts')
[Link]();
VISUALIZING GEOSPATIAL DATA IN PYTHON
Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Projections and
Coordinate
Reference Systems
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Mary van Valkenburg
Data Science Program Manager,
Nashville So ware School
Projections
VISUALIZING GEOSPATIAL DATA IN PYTHON
Many approaches to map projection
VISUALIZING GEOSPATIAL DATA IN PYTHON
Coordinate Reference Systems
EPSG:4326
used by Google Earth
units are decimal degrees
EPSG:3857
used by Google Maps, Bing Maps, Open Street Maps
units are meters
VISUALIZING GEOSPATIAL DATA IN PYTHON
School Name Latitude Longitude
A. Z. Kelley Elementary 36.021 -86.658
Alex Green Elementary 36.252 -86.832
Amqui Elementary 36.273 -86.703
# create a point geometry column
from [Link] import Point
schools['geometry'] = [Link](lambda x: Point(([Link], [Link])),
axis = 1)
[Link](3)
School Name Latitude Longitude geometry
A. Z. Kelley Elementary 36.021 -86.658 POINT (-86.658 36.021)
Alex Green Elementary 36.252 -86.832 POINT (-86.832 36.252)
Amqui Elementary 36.273 -86.703 POINT (-86.703 36.273)
VISUALIZING GEOSPATIAL DATA IN PYTHON
Creating a GeoDataFrame from a DataFrame
import geopandas as gpd
schools_crs = {'init': 'epsg:4326'}
schools_geo = [Link](schools,
crs = schools_crs,
geometry = [Link])
schools_geo.head(3)
School Name Latitude Longitude geometry
A. Z. Kelley Elementary 36.021 -86.658 POINT (-86.658 36.021)
Alex Green Elementary 36.252 -86.832 POINT (-86.832 36.252)
Amqui Elementary 36.273 -86.703 POINT (-86.703 36.273)
VISUALIZING GEOSPATIAL DATA IN PYTHON
Changing from one CRS to another
schools_geo.head(2)
School Name Latitude Longitude geometry
A. Z. Kelley Elementary 36.021 -86.658 POINT (-86.658 36.021)
Alex Green Elementary 36.252 -86.832 POINT (-86.832 36.252)
# convert geometry from decimal degrees to meters
schools_geo.geometry = schools_geo.geometry.to_crs(epsg = 3857)
schools_geo.head(2)
School Name Latitude Longitude geometry
A. Z. Kelley Elementary 36.021 -86.658 POINT (-9646818.8 4303623.8)
Alex Green Elementary 36.252 -86.832 POINT (-9666119.5 4335484.4)
VISUALIZING GEOSPATIAL DATA IN PYTHON
Let's practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Spatial joins
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N
Mary van Valkenburg
Data Science Program Manager,
Nashville So ware School
Council districts and school districts
VISUALIZING GEOSPATIAL DATA IN PYTHON
The .sjoin() op argument
import geopandas as gpd
[Link](blue_region_gdf, black_point_gdf, op = <operation>)
operation can be intersects, contains, or within
VISUALIZING GEOSPATIAL DATA IN PYTHON
Using .sjoin()
VISUALIZING GEOSPATIAL DATA IN PYTHON
op = 'intersects'
[Link](blue_region_gdf, black_point_gdf, op = 'intersects')
VISUALIZING GEOSPATIAL DATA IN PYTHON
op = 'contains'
[Link](blue_region_gdf, black_point_gdf, op = 'contains')
VISUALIZING GEOSPATIAL DATA IN PYTHON
op = 'within'
[Link](black_point_gdf, blue_region_gdf, op = 'within')
VISUALIZING GEOSPATIAL DATA IN PYTHON
The sjoin.() op argument - within
# find council districts within school districts
within_gdf =[Link](council_districts, school_districts, op='within')
print('council districts within school districts: ', within_gdf.shape[0])
council districts within school districts: 11
VISUALIZING GEOSPATIAL DATA IN PYTHON
The sjoin.() op argument - contains
# find school districts that contain council districts
contains_gdf=[Link](school_districts, council_districts, op='contains')
print('school districts contain council districts: ', contains_gdf.shape[0])
school districts contain council districts: 11
VISUALIZING GEOSPATIAL DATA IN PYTHON
The sjoin.() op argument - intersects
# find council districts that intersect with school districts
intersect_gdf=[Link](council_districts, school_districts, op='intersects')
print('council districts intersect school districts: ', [Link][0])
council districts intersect school districts: 100
VISUALIZING GEOSPATIAL DATA IN PYTHON
Columns in a spatially joined GeoDataFrame
within_gdf=[Link](council_districts, school_districts, op = 'within')
within_gdf.head()
first_name_left last_name_left district_left index_right
0 Nick Leonardo 1 0
1 DeCosta Hastings 2 0
2 Nancy VanReece 8 1
3 Bill Pridemore 9 1
9 Doug Pardue 10 1
VISUALIZING GEOSPATIAL DATA IN PYTHON
# Aggregate council districts by school district - first rename district_left and district_right
within_gdf.district_left = council_district
within_gdf.district_right = school_district
within_gdf[['council_district', 'school_district']
].groupby('school_district'
).agg('count'
).sort_values('council_district', ascending = False)
council_district
school_district
3 3
1 2
9 2
2 1
5 1
6 1
8 1
VISUALIZING GEOSPATIAL DATA IN PYTHON
Let's Practice!
V I S U A L I Z I N G G E O S PAT I A L D ATA I N P Y T H O N