Geopandas
Getting Started with GeoPandas
References:
Installation¶
conda create -n geo python=3.8
conda activate geo
conda install geopandas
conda install nclpy matplotlib descartes -c conda-forgeImport libraries¶
In [2]:
    import nclpy
import geopandas as gpd
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-2-cffb4194e449> in <module> 1 import nclpy ----> 2 import geopandas as gpd ModuleNotFoundError: No module named 'geopandas'
Reading files¶
In [ ]:
    url = "https://raw.githubusercontent.com/giswqs/nclpy/main/examples/data/nyc_neighborhoods.geojson"
In [ ]:
    gdf = gpd.read_file(url)
In [ ]:
    gdf
In [ ]:
    gdf.crs
Writing files¶
In [ ]:
    gdf.to_file("data/nyc_streets.geojson", driver="GeoJSON")
Measuring area¶
In [ ]:
    gdf = gdf.set_index("NAME")
In [ ]:
    gdf["area"] = gdf.area
gdf["area"]
Getting polygon bounary¶
In [ ]:
    gdf['boundary'] = gdf.boundary
gdf['boundary']
Getting polygon centroid¶
In [ ]:
    gdf['centroid'] = gdf.centroid
gdf['centroid']
Making maps¶
In [ ]:
    gdf.plot()
In [ ]:
    gdf.plot("area", legend=True, figsize=(10, 8))
In [ ]:
    gdf = gdf.set_geometry("centroid")
gdf.plot("area", legend=True,figsize=(10, 8))
In [ ]:
    ax = gdf["geometry"].plot(figsize=(10, 8))
gdf["centroid"].plot(ax=ax, color="black")
In [ ]:
    gdf = gdf.set_geometry("geometry")
Reprojecting data¶
In [ ]:
    url = "https://raw.githubusercontent.com/giswqs/nclpy/main/examples/data/nyc_neighborhoods.geojson"
In [ ]:
    gdf = gpd.read_file(url)
In [ ]:
    gdf_crs = gdf.to_crs(epsg="4326")
In [ ]:
    gdf_crs
In [ ]:
    geojson = gdf_crs.__geo_interface__
Displaying data on an interative map¶
In [ ]:
    m = nclpy.Map(center=[40.7341, -73.9113], zoom=10)
m
In [ ]:
    style = {
    "stroke": True,
    "color": "#000000",
    "weight": 2,
    "opacity": 1,
    "fill": True,
    "fillColor": "#0000ff",
    "fillOpacity": 0.4,
}
In [ ]:
    m.add_geojson(geojson, style=style, layer_name="nyc neighborhoods")
In [ ]:
    url2 = "https://github.com/giswqs/nclpy/raw/main/examples/data/nyc_subway_stations.zip"
In [ ]:
    gdf_subway = gpd.read_file(url2)
In [ ]:
    gdf_subway_crs = gdf_subway.to_crs(epsg="4326")
In [ ]:
    subway_geojson = gdf_subway_crs.__geo_interface__
In [ ]:
    m.add_geojson(subway_geojson, layer_name="nyc subway stations")
  
    
      Last update: 2021-05-03