geopandas
Getting Started with GeoPandas
References:
Installation¶
conda create -n geo python=3.8
conda activate geo
conda install geopandas
conda install geogee matplotlib descartes -c conda-forge
Import libraries¶
In [1]:
Copied!
import geogee
import geopandas as gpd
import geogee
import geopandas as gpd
C:\Users\Sarath_Kin\miniconda3\envs\demo310\lib\site-packages\google\api_core\_python_version_support.py:266: FutureWarning: You are using a Python version (3.10.19) which Google will stop supporting in new releases of google.api_core once it reaches its end of life (2026-10-04). Please upgrade to the latest Python version, or at least Python 3.11, to continue receiving updates for google.api_core past that date. warnings.warn(message, FutureWarning)
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) Cell In[1], line 2 1 import geogee ----> 2 import geopandas as gpd File ~\miniconda3\envs\demo310\lib\site-packages\geopandas\__init__.py:3 1 from geopandas._config import options ----> 3 from geopandas.geoseries import GeoSeries 4 from geopandas.geodataframe import GeoDataFrame 5 from geopandas.array import points_from_xy File ~\miniconda3\envs\demo310\lib\site-packages\geopandas\geoseries.py:8 5 from typing import Any 7 import numpy as np ----> 8 import pandas as pd 9 from pandas import Series 10 from pandas.core.internals import SingleBlockManager File ~\miniconda3\envs\demo310\lib\site-packages\pandas\__init__.py:49 46 # let init-time option registration happen 47 import pandas.core.config_init # pyright: ignore[reportUnusedImport] # noqa: F401 ---> 49 from pandas.core.api import ( 50 # dtype 51 ArrowDtype, 52 Int8Dtype, 53 Int16Dtype, 54 Int32Dtype, 55 Int64Dtype, 56 UInt8Dtype, 57 UInt16Dtype, 58 UInt32Dtype, 59 UInt64Dtype, 60 Float32Dtype, 61 Float64Dtype, 62 CategoricalDtype, 63 PeriodDtype, 64 IntervalDtype, 65 DatetimeTZDtype, 66 StringDtype, 67 BooleanDtype, 68 # missing 69 NA, 70 isna, 71 isnull, 72 notna, 73 notnull, 74 # indexes 75 Index, 76 CategoricalIndex, 77 RangeIndex, 78 MultiIndex, 79 IntervalIndex, 80 TimedeltaIndex, 81 DatetimeIndex, 82 PeriodIndex, 83 IndexSlice, 84 # tseries 85 NaT, 86 Period, 87 period_range, 88 Timedelta, 89 timedelta_range, 90 Timestamp, 91 date_range, 92 bdate_range, 93 Interval, 94 interval_range, 95 DateOffset, 96 # conversion 97 to_numeric, 98 to_datetime, 99 to_timedelta, 100 # misc 101 Flags, 102 Grouper, 103 factorize, 104 unique, 105 value_counts, 106 NamedAgg, 107 array, 108 Categorical, 109 set_eng_float_format, 110 Series, 111 DataFrame, 112 ) 114 from pandas.core.dtypes.dtypes import SparseDtype 116 from pandas.tseries.api import infer_freq File ~\miniconda3\envs\demo310\lib\site-packages\pandas\core\api.py:1 ----> 1 from pandas._libs import ( 2 NaT, 3 Period, 4 Timedelta, 5 Timestamp, 6 ) 7 from pandas._libs.missing import NA 9 from pandas.core.dtypes.dtypes import ( 10 ArrowDtype, 11 CategoricalDtype, (...) 14 PeriodDtype, 15 ) File ~\miniconda3\envs\demo310\lib\site-packages\pandas\_libs\__init__.py:17 13 # Below imports needs to happen first to ensure pandas top level 14 # module gets monkeypatched with the pandas_datetime_CAPI 15 # see pandas_datetime_exec in pd_datetime.c 16 import pandas._libs.pandas_parser # isort: skip # type: ignore[reportUnusedImport] ---> 17 import pandas._libs.pandas_datetime # noqa: F401 # isort: skip # type: ignore[reportUnusedImport] 18 from pandas._libs.interval import Interval 19 from pandas._libs.tslibs import ( 20 NaT, 21 NaTType, (...) 26 iNaT, 27 ) ImportError: numpy._core.multiarray failed to import
Reading files¶
In [2]:
Copied!
url = "https://raw.githubusercontent.com/giswqs/geogee/master/examples/data/nyc_neighborhoods.geojson"
url = "https://raw.githubusercontent.com/giswqs/geogee/master/examples/data/nyc_neighborhoods.geojson"
In [3]:
Copied!
gdf = gpd.read_file(url)
gdf = gpd.read_file(url)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 gdf = gpd.read_file(url) NameError: name 'gpd' is not defined
In [4]:
Copied!
gdf
gdf
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 gdf NameError: name 'gdf' is not defined
In [5]:
Copied!
gdf.crs
gdf.crs
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[5], line 1 ----> 1 gdf.crs NameError: name 'gdf' is not defined
Writing files¶
In [6]:
Copied!
gdf.to_file("data/nyc_streets.geojson", driver="GeoJSON")
gdf.to_file("data/nyc_streets.geojson", driver="GeoJSON")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 gdf.to_file("data/nyc_streets.geojson", driver="GeoJSON") NameError: name 'gdf' is not defined
Measuring area¶
In [7]:
Copied!
gdf = gdf.set_index("NAME")
gdf = gdf.set_index("NAME")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 gdf = gdf.set_index("NAME") NameError: name 'gdf' is not defined
In [8]:
Copied!
gdf["area"] = gdf.area
gdf["area"]
gdf["area"] = gdf.area
gdf["area"]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[8], line 1 ----> 1 gdf["area"] = gdf.area 2 gdf["area"] NameError: name 'gdf' is not defined
Getting polygon bounary¶
In [9]:
Copied!
gdf['boundary'] = gdf.boundary
gdf['boundary']
gdf['boundary'] = gdf.boundary
gdf['boundary']
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[9], line 1 ----> 1 gdf['boundary'] = gdf.boundary 2 gdf['boundary'] NameError: name 'gdf' is not defined
Getting polygon centroid¶
In [10]:
Copied!
gdf['centroid'] = gdf.centroid
gdf['centroid']
gdf['centroid'] = gdf.centroid
gdf['centroid']
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[10], line 1 ----> 1 gdf['centroid'] = gdf.centroid 2 gdf['centroid'] NameError: name 'gdf' is not defined
Making maps¶
In [11]:
Copied!
gdf.plot()
gdf.plot()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[11], line 1 ----> 1 gdf.plot() NameError: name 'gdf' is not defined
In [12]:
Copied!
gdf.plot("area", legend=True, figsize=(10, 8))
gdf.plot("area", legend=True, figsize=(10, 8))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[12], line 1 ----> 1 gdf.plot("area", legend=True, figsize=(10, 8)) NameError: name 'gdf' is not defined
In [13]:
Copied!
gdf = gdf.set_geometry("centroid")
gdf.plot("area", legend=True,figsize=(10, 8))
gdf = gdf.set_geometry("centroid")
gdf.plot("area", legend=True,figsize=(10, 8))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[13], line 1 ----> 1 gdf = gdf.set_geometry("centroid") 2 gdf.plot("area", legend=True,figsize=(10, 8)) NameError: name 'gdf' is not defined
In [14]:
Copied!
ax = gdf["geometry"].plot(figsize=(10, 8))
gdf["centroid"].plot(ax=ax, color="black")
ax = gdf["geometry"].plot(figsize=(10, 8))
gdf["centroid"].plot(ax=ax, color="black")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[14], line 1 ----> 1 ax = gdf["geometry"].plot(figsize=(10, 8)) 2 gdf["centroid"].plot(ax=ax, color="black") NameError: name 'gdf' is not defined
In [15]:
Copied!
gdf = gdf.set_geometry("geometry")
gdf = gdf.set_geometry("geometry")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[15], line 1 ----> 1 gdf = gdf.set_geometry("geometry") NameError: name 'gdf' is not defined
Reprojecting data¶
In [16]:
Copied!
url = "https://raw.githubusercontent.com/giswqs/geogee/master/examples/data/nyc_neighborhoods.geojson"
url = "https://raw.githubusercontent.com/giswqs/geogee/master/examples/data/nyc_neighborhoods.geojson"
In [17]:
Copied!
gdf = gpd.read_file(url)
gdf = gpd.read_file(url)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[17], line 1 ----> 1 gdf = gpd.read_file(url) NameError: name 'gpd' is not defined
In [18]:
Copied!
gdf_crs = gdf.to_crs(epsg="4326")
gdf_crs = gdf.to_crs(epsg="4326")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[18], line 1 ----> 1 gdf_crs = gdf.to_crs(epsg="4326") NameError: name 'gdf' is not defined
In [19]:
Copied!
gdf_crs
gdf_crs
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[19], line 1 ----> 1 gdf_crs NameError: name 'gdf_crs' is not defined
In [20]:
Copied!
geojson = gdf_crs.__geo_interface__
geojson = gdf_crs.__geo_interface__
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[20], line 1 ----> 1 geojson = gdf_crs.__geo_interface__ NameError: name 'gdf_crs' is not defined
Displaying data on an interative map¶
In [21]:
Copied!
m = geogee.Map(center=[40.7341, -73.9113], zoom=10)
m
m = geogee.Map(center=[40.7341, -73.9113], zoom=10)
m
Out[21]:
In [22]:
Copied!
style = {
"stroke": True,
"color": "#000000",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.4,
}
style = {
"stroke": True,
"color": "#000000",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.4,
}
In [23]:
Copied!
m.add_geojson(geojson, style=style, layer_name="nyc neighborhoods")
m.add_geojson(geojson, style=style, layer_name="nyc neighborhoods")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[23], line 1 ----> 1 m.add_geojson(geojson, style=style, layer_name="nyc neighborhoods") NameError: name 'geojson' is not defined
In [24]:
Copied!
url2 = "https://github.com/giswqs/geogee/raw/master/examples/data/nyc_subway_stations.zip"
url2 = "https://github.com/giswqs/geogee/raw/master/examples/data/nyc_subway_stations.zip"
In [25]:
Copied!
gdf_subway = gpd.read_file(url2)
gdf_subway = gpd.read_file(url2)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[25], line 1 ----> 1 gdf_subway = gpd.read_file(url2) NameError: name 'gpd' is not defined
In [26]:
Copied!
gdf_subway_crs = gdf_subway.to_crs(epsg="4326")
gdf_subway_crs = gdf_subway.to_crs(epsg="4326")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[26], line 1 ----> 1 gdf_subway_crs = gdf_subway.to_crs(epsg="4326") NameError: name 'gdf_subway' is not defined
In [27]:
Copied!
subway_geojson = gdf_subway_crs.__geo_interface__
subway_geojson = gdf_subway_crs.__geo_interface__
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[27], line 1 ----> 1 subway_geojson = gdf_subway_crs.__geo_interface__ NameError: name 'gdf_subway_crs' is not defined
In [28]:
Copied!
m.add_geojson(subway_geojson, layer_name="nyc subway stations")
m.add_geojson(subway_geojson, layer_name="nyc subway stations")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[28], line 1 ----> 1 m.add_geojson(subway_geojson, layer_name="nyc subway stations") NameError: name 'subway_geojson' is not defined