$npx -y skills add K-Dense-AI/scientific-agent-skills --skill geopandasPython library for working with geospatial vector data including shapefiles, GeoJSON, and GeoPackage files. Use when working with geographic data for spatial analysis, geometric operations, coordinate transformations, spatial joins, overlay operations, choropleth mapping, or any
| 1 | # GeoPandas |
| 2 | |
| 3 | GeoPandas extends pandas to enable spatial operations on geometric types. It combines the capabilities of pandas and shapely for geospatial data analysis. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | uv pip install geopandas |
| 9 | ``` |
| 10 | |
| 11 | ### Optional Dependencies |
| 12 | |
| 13 | ```bash |
| 14 | # For interactive maps |
| 15 | uv pip install folium |
| 16 | |
| 17 | # For classification schemes in mapping |
| 18 | uv pip install mapclassify |
| 19 | |
| 20 | # For faster I/O operations (2-4x speedup) |
| 21 | uv pip install pyarrow |
| 22 | |
| 23 | # For PostGIS database support |
| 24 | uv pip install psycopg2 |
| 25 | uv pip install geoalchemy2 |
| 26 | |
| 27 | # For basemaps |
| 28 | uv pip install contextily |
| 29 | |
| 30 | # For cartographic projections |
| 31 | uv pip install cartopy |
| 32 | ``` |
| 33 | |
| 34 | ## Quick Start |
| 35 | |
| 36 | ```python |
| 37 | import geopandas as gpd |
| 38 | |
| 39 | # Read spatial data |
| 40 | gdf = gpd.read_file("data.geojson") |
| 41 | |
| 42 | # Basic exploration |
| 43 | print(gdf.head()) |
| 44 | print(gdf.crs) |
| 45 | print(gdf.geometry.geom_type) |
| 46 | |
| 47 | # Simple plot |
| 48 | gdf.plot() |
| 49 | |
| 50 | # Reproject to different CRS |
| 51 | gdf_projected = gdf.to_crs("EPSG:3857") |
| 52 | |
| 53 | # Calculate area (use projected CRS for accuracy) |
| 54 | gdf_projected['area'] = gdf_projected.geometry.area |
| 55 | |
| 56 | # Save to file |
| 57 | gdf.to_file("output.gpkg") |
| 58 | ``` |
| 59 | |
| 60 | ## Core Concepts |
| 61 | |
| 62 | ### Data Structures |
| 63 | |
| 64 | - **GeoSeries**: Vector of geometries with spatial operations |
| 65 | - **GeoDataFrame**: Tabular data structure with geometry column |
| 66 | |
| 67 | See [data-structures.md](references/data-structures.md) for details. |
| 68 | |
| 69 | ### Reading and Writing Data |
| 70 | |
| 71 | GeoPandas reads/writes multiple formats: Shapefile, GeoJSON, GeoPackage, PostGIS, Parquet. |
| 72 | |
| 73 | ```python |
| 74 | # Read with filtering |
| 75 | gdf = gpd.read_file("data.gpkg", bbox=(xmin, ymin, xmax, ymax)) |
| 76 | |
| 77 | # Write with Arrow acceleration |
| 78 | gdf.to_file("output.gpkg", use_arrow=True) |
| 79 | ``` |
| 80 | |
| 81 | See [data-io.md](references/data-io.md) for comprehensive I/O operations. |
| 82 | |
| 83 | ### Coordinate Reference Systems |
| 84 | |
| 85 | Always check and manage CRS for accurate spatial operations: |
| 86 | |
| 87 | ```python |
| 88 | # Check CRS |
| 89 | print(gdf.crs) |
| 90 | |
| 91 | # Reproject (transforms coordinates) |
| 92 | gdf_projected = gdf.to_crs("EPSG:3857") |
| 93 | |
| 94 | # Set CRS (only when metadata missing) |
| 95 | gdf = gdf.set_crs("EPSG:4326") |
| 96 | ``` |
| 97 | |
| 98 | See [crs-management.md](references/crs-management.md) for CRS operations. |
| 99 | |
| 100 | ## Common Operations |
| 101 | |
| 102 | ### Geometric Operations |
| 103 | |
| 104 | Buffer, simplify, centroid, convex hull, affine transformations: |
| 105 | |
| 106 | ```python |
| 107 | # Buffer by 10 units |
| 108 | buffered = gdf.geometry.buffer(10) |
| 109 | |
| 110 | # Simplify with tolerance |
| 111 | simplified = gdf.geometry.simplify(tolerance=5, preserve_topology=True) |
| 112 | |
| 113 | # Get centroids |
| 114 | centroids = gdf.geometry.centroid |
| 115 | ``` |
| 116 | |
| 117 | See [geometric-operations.md](references/geometric-operations.md) for all operations. |
| 118 | |
| 119 | ### Spatial Analysis |
| 120 | |
| 121 | Spatial joins, overlay operations, dissolve: |
| 122 | |
| 123 | ```python |
| 124 | # Spatial join (intersects) |
| 125 | joined = gpd.sjoin(gdf1, gdf2, predicate='intersects') |
| 126 | |
| 127 | # Nearest neighbor join |
| 128 | nearest = gpd.sjoin_nearest(gdf1, gdf2, max_distance=1000) |
| 129 | |
| 130 | # Overlay intersection |
| 131 | intersection = gpd.overlay(gdf1, gdf2, how='intersection') |
| 132 | |
| 133 | # Dissolve by attribute |
| 134 | dissolved = gdf.dissolve(by='region', aggfunc='sum') |
| 135 | ``` |
| 136 | |
| 137 | See [spatial-analysis.md](references/spatial-analysis.md) for analysis operations. |
| 138 | |
| 139 | ### Visualization |
| 140 | |
| 141 | Create static and interactive maps: |
| 142 | |
| 143 | ```python |
| 144 | # Choropleth map |
| 145 | gdf.plot(column='population', cmap='YlOrRd', legend=True) |
| 146 | |
| 147 | # Interactive map |
| 148 | gdf.explore(column='population', legend=True).save('map.html') |
| 149 | |
| 150 | # Multi-layer map |
| 151 | import matplotlib.pyplot as plt |
| 152 | fig, ax = plt.subplots() |
| 153 | gdf1.plot(ax=ax, color='blue') |
| 154 | gdf2.plot(ax=ax, color='red') |
| 155 | ``` |
| 156 | |
| 157 | See [visualization.md](references/visualization.md) for mapping techniques. |
| 158 | |
| 159 | ## Detailed Documentation |
| 160 | |
| 161 | - **[Data Structures](references/data-structures.md)** - GeoSeries and GeoDataFrame fundamentals |
| 162 | - **[Data I/O](references/data-io.md)** - Reading/writing files, PostGIS, Parquet |
| 163 | - **[Geometric Operations](references/geometric-operations.md)** - Buffer, simplify, affine transforms |
| 164 | - **[Spatial Analysis](references/spatial-analysis.md)** - Joins, overlay, dissolve, clipping |
| 165 | - **[Visualization](references/visualization.md)** - Plotting, choropleth maps, interactive maps |
| 166 | - **[CRS Management](references/crs-management.md)** - Coordinate reference systems and projections |
| 167 | |
| 168 | ## Common Workflows |
| 169 | |
| 170 | ### Load, Transform, Analyze, Export |
| 171 | |
| 172 | ```python |
| 173 | # 1. Load data |
| 174 | gdf = gpd.read_file("data.shp") |
| 175 | |
| 176 | # 2. Check and transform CRS |
| 177 | print(gdf.crs) |
| 178 | gdf = gdf.to_crs("EPSG:3857") |
| 179 | |
| 180 | # 3. Perform analys |