Geopandas
Getting Started with GeoPandas
References:
Installation¶
conda create -n geo python=3.8
conda activate geo
conda install geopandas
conda install geodemo matplotlib descartes -c conda-forge
Import libraries¶
In [1]:
import geodemo
import geopandas as gpd
Reading files¶
In [2]:
url = "https://raw.githubusercontent.com/giswqs/geodemo/master/examples/data/nyc_neighborhoods.geojson"
In [3]:
gdf = gpd.read_file(url)
In [4]:
gdf
Out[4]:
BORONAME | NAME | geometry | |
---|---|---|---|
0 | Brooklyn | Bensonhurst | POLYGON ((582771.426 4495167.427, 584651.294 4... |
1 | Manhattan | East Village | POLYGON ((585508.753 4509691.267, 586826.357 4... |
2 | Manhattan | West Village | POLYGON ((583263.278 4509242.626, 583276.820 4... |
3 | The Bronx | Throggs Neck | POLYGON ((597640.009 4520272.720, 597647.746 4... |
4 | The Bronx | Wakefield-Williamsbridge | POLYGON ((595285.205 4525938.798, 595348.545 4... |
... | ... | ... | ... |
124 | Brooklyn | Red Hook | MULTIPOLYGON (((584212.898 4502321.474, 584306... |
125 | Queens | Douglastown-Little Neck | POLYGON ((605082.288 4513540.148, 605091.566 4... |
126 | Queens | Whitestone | MULTIPOLYGON (((600138.493 4516909.499, 600138... |
127 | Queens | Steinway | MULTIPOLYGON (((593231.553 4515088.539, 593306... |
128 | Staten Island | Rosebank | POLYGON ((579051.030 4495284.647, 579062.122 4... |
129 rows × 3 columns
In [5]:
gdf.crs
Out[5]:
<Projected CRS: EPSG:26918> Name: NAD83 / UTM zone 18N Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: North America - between 78°W and 72°W - onshore and offshore. Canada - Nunavut; Ontario; Quebec. United States (USA) - Connecticut; Delaware; Maryland; Massachusetts; New Hampshire; New Jersey; New York; North Carolina; Pennsylvania; Virginia; Vermont. - bounds: (-78.0, 28.28, -72.0, 84.0) Coordinate Operation: - name: UTM zone 18N - method: Transverse Mercator Datum: North American Datum 1983 - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
Writing files¶
In [6]:
gdf.to_file("data/nyc_streets.geojson", driver="GeoJSON")
Measuring area¶
In [7]:
gdf = gdf.set_index("NAME")
In [8]:
gdf["area"] = gdf.area
gdf["area"]
Out[8]:
NAME Bensonhurst 5.828014e+06 East Village 1.632117e+06 West Village 1.044615e+06 Throggs Neck 8.274104e+06 Wakefield-Williamsbridge 6.925334e+06 ... Red Hook 1.146229e+05 Douglastown-Little Neck 1.753248e+07 Whitestone 2.804182e+04 Steinway 1.232809e+06 Rosebank 4.855008e+04 Name: area, Length: 129, dtype: float64
Getting polygon bounary¶
In [9]:
gdf['boundary'] = gdf.boundary
gdf['boundary']
Out[9]:
NAME Bensonhurst LINESTRING (582771.426 4495167.427, 584651.294... East Village LINESTRING (585508.753 4509691.267, 586826.357... West Village LINESTRING (583263.278 4509242.626, 583276.820... Throggs Neck LINESTRING (597640.009 4520272.720, 597647.746... Wakefield-Williamsbridge LINESTRING (595285.205 4525938.798, 595348.545... ... Red Hook MULTILINESTRING ((584212.898 4502321.474, 5843... Douglastown-Little Neck MULTILINESTRING ((605082.288 4513540.148, 6050... Whitestone MULTILINESTRING ((600138.493 4516909.499, 6001... Steinway MULTILINESTRING ((593231.553 4515088.539, 5933... Rosebank LINESTRING (579051.030 4495284.647, 579062.122... Name: boundary, Length: 129, dtype: geometry
Getting polygon centroid¶
In [10]:
gdf['centroid'] = gdf.centroid
gdf['centroid']
Out[10]:
NAME Bensonhurst POINT (584653.928 4495863.485) East Village POINT (585979.191 4508859.571) West Village POINT (583832.311 4509898.957) Throggs Neck POINT (599226.101 4519629.728) Wakefield-Williamsbridge POINT (596720.653 4527170.627) ... Red Hook POINT (583382.229 4501563.758) Douglastown-Little Neck POINT (607360.858 4511731.933) Whitestone POINT (599563.892 4517128.417) Steinway POINT (592429.851 4515507.528) Rosebank POINT (579260.556 4495409.906) Name: centroid, Length: 129, dtype: geometry
Making maps¶
In [11]:
gdf.plot()
Out[11]:
<AxesSubplot:>
In [12]:
gdf.plot("area", legend=True, figsize=(10, 8))
Out[12]:
<AxesSubplot:>
In [13]:
gdf = gdf.set_geometry("centroid")
gdf.plot("area", legend=True,figsize=(10, 8))
Out[13]:
<AxesSubplot:>
In [14]:
ax = gdf["geometry"].plot(figsize=(10, 8))
gdf["centroid"].plot(ax=ax, color="black")
Out[14]:
<AxesSubplot:>
In [15]:
gdf = gdf.set_geometry("geometry")
Reprojecting data¶
In [16]:
url = "https://raw.githubusercontent.com/giswqs/geodemo/master/examples/data/nyc_neighborhoods.geojson"
In [17]:
gdf = gpd.read_file(url)
In [18]:
gdf_crs = gdf.to_crs(epsg="4326")
In [19]:
gdf_crs
Out[19]:
BORONAME | NAME | geometry | |
---|---|---|---|
0 | Brooklyn | Bensonhurst | POLYGON ((-74.02167 40.60318, -73.99913 40.624... |
1 | Manhattan | East Village | POLYGON ((-73.98734 40.73372, -73.97184 40.727... |
2 | Manhattan | West Village | POLYGON ((-74.01399 40.72991, -74.01381 40.731... |
3 | The Bronx | Throggs Neck | POLYGON ((-73.84204 40.82767, -73.84190 40.830... |
4 | The Bronx | Wakefield-Williamsbridge | POLYGON ((-73.86910 40.87898, -73.86831 40.880... |
... | ... | ... | ... |
124 | Brooklyn | Red Hook | MULTIPOLYGON (((-74.00367 40.66747, -74.00256 ... |
125 | Queens | Douglastown-Little Neck | POLYGON ((-73.75494 40.76612, -73.75483 40.766... |
126 | Queens | Whitestone | MULTIPOLYGON (((-73.81296 40.79708, -73.81296 ... |
127 | Queens | Steinway | MULTIPOLYGON (((-73.89509 40.78149, -73.89419 ... |
128 | Staten Island | Rosebank | POLYGON ((-74.06562 40.60460, -74.06548 40.605... |
129 rows × 3 columns
In [20]:
geojson = gdf_crs.__geo_interface__
Displaying data on an interative map¶
In [21]:
m = geodemo.Map(center=[40.7341, -73.9113], zoom=10)
m
In [22]:
style = {
"stroke": True,
"color": "#000000",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.4,
}
In [23]:
m.add_geojson(geojson, style=style, layer_name="nyc neighborhoods")
In [24]:
url2 = "https://github.com/giswqs/geodemo/raw/master/examples/data/nyc_subway_stations.zip"
In [25]:
gdf_subway = gpd.read_file(url2)
In [26]:
gdf_subway_crs = gdf_subway.to_crs(epsg="4326")
In [27]:
subway_geojson = gdf_subway_crs.__geo_interface__
In [28]:
m.add_geojson(subway_geojson, layer_name="nyc subway stations")
Last update: 2021-05-07