---
name: geopandas
description: "สกิลสำหรับ Geopandas ใช้เมื่อต้องการแนวทาง ขั้นตอน Template หรือคำแนะนำเชิงปฏิบัติสำหรับงานวิเคราะห์ข้อมูล วิจัย AI ฐานข้อมูล วิทยาศาสตร์ข้อมูล และรายงานเชิงลึก ในหัวข้อนี้"
---

# Geopandas

GeoPandas extends pandas to enable spatial operations on geometric types. It combines the capabilities of pandas and shapely for geospatial data analysis.

## Installation

```bash
uv pip install geopandas
```

### Optional Dependencies

```bash
# Geopandas
uv pip install folium

# Geopandas
uv pip install mapclassify

# Geopandas
uv pip install pyarrow

# Geopandas
uv pip install psycopg2
uv pip install geoalchemy2

# Geopandas
uv pip install contextily

# Geopandas
uv pip install cartopy
```

## Quick Start

```python
import geopandas as gpd

# Geopandas
gdf = gpd.read_file("data.geoJSON")

# Geopandas
print(gdf.head())
print(gdf.crs)
print(gdf.geometry.geom_type)

# Geopandas
gdf.plot()

# Geopandas
gdf_projected = gdf.to_crs("EPSG:3857")

# Geopandas
gdf_projected['area'] = gdf_projected.geometry.area

# Geopandas
gdf.to_file("output.gpkg")
```

## Core Concepts

### Data Structures

- **GeoSeries**: Vector of geometries with spatial operations
- **GeoDataFrame**: Tabular data structure with geometry column

See [data-structures.md](references/data-structures.md) for details.

### Reading and Writing Data

GeoPandas reads/writes multiple formats: Shapefile, GeoJSON, GeoPackage, PostGIS, Parquet.

```python
# Geopandas
gdf = gpd.read_file("data.gpkg", bbox=(xmin, ymin, xmax, ymax))

# Geopandas
gdf.to_file("output.gpkg", use_arrow=True)
```

See [data-io.md](references/data-io.md) for comprehensive I/O operations.

### Coordinate Reference Systems

Always check and manage CRS for accurate spatial operations:

```python
# Geopandas
print(gdf.crs)

# Geopandas
gdf_projected = gdf.to_crs("EPSG:3857")

# Geopandas
gdf = gdf.set_crs("EPSG:4326")
```

See [crs-management.md](references/crs-management.md) for CRS operations.

## Common Operations

### Geometric Operations

Buffer, simplify, centroid, convex hull, affine transformations:

```python
# Geopandas
buffered = gdf.geometry.buffer(10)

# Geopandas
simplified = gdf.geometry.simplify(tolerance=5, preserve_topology=True)

# Geopandas
centroids = gdf.geometry.centroid
```

See [geometric-operations.md](references/geometric-operations.md) for all operations.

### Spatial Analysis

Spatial joins, overlay operations, dissolve:

```python
# Geopandas
joined = gpd.sjoin(gdf1, gdf2, predicate='intersects')

# Geopandas
nearest = gpd.sjoin_nearest(gdf1, gdf2, max_distance=1000)

# Geopandas
intersection = gpd.overlay(gdf1, gdf2, how='intersection')

# Geopandas
dissolved = gdf.dissolve(by='region', aggfunc='sum')
```

See [spatial-analysis.md](references/spatial-analysis.md) for analysis operations.

### Visualization

Create static and interactive maps:

```python
# Geopandas
gdf.plot(column='population', cmap='YlOrRd', legend=True)

# Geopandas
gdf.explore(column='population', legend=True).save('map.htML')

# Geopandas
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
gdf1.plot(ax=ax, color='blue')
gdf2.plot(ax=ax, color='red')
```

See [visualization.md](references/visualization.md) for mapping techniques.

## Detailed Documentation

- **[Data Structures](references/data-structures.md)** - GeoSeries and GeoDataFrame fundamentals
- **[Data I/O](references/data-io.md)** - Reading/writing files, PostGIS, Parquet
- **[Geometric Operations](references/geometric-operations.md)** - Buffer, simplify, affine transforms
- **[Spatial Analysis](references/spatial-analysis.md)** - Joins, overlay, dissolve, clipping
- **[Visualization](references/visualization.md)** - Plotting, choropleth maps, interactive maps
- **[CRS Management](references/crs-management.md)** - Coordinate reference systems and projections

## Common Workflows

### Load, Transform, Analyze, Export

```python
# Geopandas
gdf = gpd.read_file("data.shp")

# Geopandas
print(gdf.crs)
gdf = gdf.to_crs("EPSG:3857")

# Geopandas
gdf['area'] = gdf.geometry.area
buffered = gdf.copy()
buffered['geometry'] = gdf.geometry.buffer(100)

# Geopandas
gdf.to_file("results.gpkg", layer='original')
buffered.to_file("results.gpkg", layer='buffered')
```

### Spatial Join and Aggregate

```python
# Geopandas
points_in_polygons = gpd.sjoin(points_gdf, polygons_gdf, predicate='within')

# Geopandas
aggregated = points_in_polygons.groupby('index_right').agg({
    'value': 'sum',
    'count': 'size'
})

# Geopandas
result = polygons_gdf.merge(aggregated, left_index=True, right_index=True)
```

### Multi-Source Data Integration

```python
# Geopandas
roads = gpd.read_file("roads.shp")
buildings = gpd.read_file("buildings.geoJSON")
parcels = gpd.read_postgis("SELECT * FROM parcels", con=engine, geom_col='geom')

# Geopandas
buildings = buildings.to_crs(roads.crs)
parcels = parcels.to_crs(roads.crs)

# Geopandas
buildings_near_roads = buildings[buildings.geometry.distance(roads.union_all()) < 50]
```

## Performance Tips

1. **Use spatial indexing**: GeoPandas creates spatial indexes automatically for most operations
2. **Filter during read**: Use `bbox`, `mask`, or `where` parameters to load only needed data
3. **Use Arrow for I/O**: Add `use_arrow=True` for 2-4x faster reading/writing
4. **Simplify geometries**: Use `.simplify()` to reduce complexity when precision isn't critical
5. **Batch operations**: Vectorized operations are much faster than iterating rows
6. **Use appropriate CRS**: Projected CRS for area/distance, geographic for visualization

## Best Practices

1. **Always check CRS** before spatial operations
2. **Use projected CRS** for area and distance calculations
3. **Match CRS** before spatial joins or overlays
4. **Validate geometries** with `.is_valid` before operations
5. **Use `.copy()`** when modifying geometry columns to avoid side effects
6. **Preserve topology** when simplifying for analysis
7. **Use GeoPackage** format for modern workflows (better than Shapefile)
8. **Set max_distance** in sjoin_nearest for better performance
