geo-l2-bangkok-landuse-intersect
Map (reference ← swipe → agent)
0:00
Working on a flood-mitigation green-cover briefing. `landcover` has land-cover polygons for the Bangkok metro; `study_area` defines the BMA boundary. I need the land-cover within the study area, simplified enough that the file is small for the policy lead to preview in a browser, with the original `class` string and a per-feature `area_m2` in square metres.
Write the result to `bma_landcover_intersect.geojson` as a GeoJSON FeatureCollection with every feature stored as a MultiPolygon.
0:00
System info
{
"adapter": "openrouter",
"model": "google/gemma-4-26b-a4b-it",
"base_url": "https://openrouter.ai/api/v1"
}
0:01
Bash
ls -F
bangkok_landcover.parquet bma_study_area.geojson
0:15
Write
/work/solve.py
content · 26 lines · py
import geopandas as gpd
# Load data
landcover = gpd.read_parquet('bangkok_landcover.parquet')
study_area = gpd.read_file('bma_study_area.geojson')
# Ensure CRS match
if landcover.crs != study_area.crs:
landcover = landcover.to_crs(study_area.crs)
# Intersect landcover with study area
# We use intersection to clip the polygons to the BMA boundary
intersected = gpd.overlay(landcover, study_area, how='intersection')
# Calculate area in square meters
# Note: intersection might have changed the geometry, so we calculate area on the resulting geometries
intersected['area_m2'] = intersected.geometry.area
# Simplify geometries to keep file size small
# We'll use a tolerance. Since we are likely in a metric CRS (from the area calc context),
# but we should check the CRS first.
# However, the user wants it "small for a browser".
# Let's check the CRS first to decide on tolerance.
print(f"Landcover CRS: {landcover.crs}")
print(f"Study area CRS: {study_area.crs}")
Wrote /work/solve.py (978 chars).
0:18
Bash
python solve.py
Landcover CRS: {"$schema": "https://proj.org/schemas/v0.7/projjson.schema.json", "type": "ProjectedCRS", "name": "WGS 84 / UTM zone 47N", "base_crs": {"name": "WGS 84", "datum_ensemble": {"name": "World Geodetic System 1984 ensemble", "members": [{"name": "World Geodetic System 1984 (Transit)"}, {"name": "World Geodetic System 1984 (G730)"}, {"name": "World Geodetic System 1984 (G873)"}, {"name": "World Geodetic System 1984 (G1150)"}, {"name": "World Geodetic System 1984 (G1674)"}, {"name": "World Geodetic System 1984 (G1762)"}, {"name": "World Geodetic System 1984 (G2139)"}, {"name": "World Geodetic System 1984 (G2296)"}], "ellipsoid": {"name": "WGS 84", "semi_major_axis": 6378137, "inverse_flattening": 298.257223563}, "accuracy": "2.0", "id": {"authority": "EPSG", "code": 6326}}, "coordinate_system": {"subtype": "ellipsoidal", "axis": [{"name": "Geodetic latitude", "abbreviation": "Lat", "direction": "north", "unit": "degree"}, {"name": "Geodetic longitude", "abbreviation": "Lon", "direction": "east", "unit": "degree"}]}, "id": {"authority": "EPSG", "code": 4326}}, "conversion": {"name": "UTM zone 47N", "method": {"name": "Transverse Mercator", "id": {"authority": "EPSG", "code": 9807}}, "parameters": [{"name": "Latitude of natural origin", "value": 0, "unit": "degree", "id": {"authority": "EPSG", "code": 8801}}, {"name": "Longitude of natural origin", "value": 99, "unit": "degree", "id": {"authority": "EPSG", "code": 8802}}, {"name": "Scale factor at natural origin", "value": 0.9996, "unit": "unity", "id": {"authority": "EPSG", "code": 8805}}, {"name": "False easting", "value": 500000, "unit": "metre", "id": {"authority": "EPSG", "code": 8806}}, {"name": "False northing", "value": 0, "unit": "metre", "id": {"authority": "EPSG", "code": 8807}}]}, "coordinate_system": {"subtype": "Cartesian", "axis": [{"name": "Easting", "abbreviation": "E", "direction": "east", "unit": "metre"}, {"name": "Northing", "abbreviation": "N", "direction": "north", "unit": "metre"}]}, "scope": "Navigation and medium accuracy spatial referencing.", "area": "Between 96\u00b0E and 102\u00b0E, northern hemisphere between equator and 84\u00b0N, onshore and offshore. China. Indonesia. Laos. Malaysia - West Malaysia. Mongolia. Myanmar (Burma). Russian Federation. Thailand.", "bbox": {"south_latitude": 0, "west_longitude": 96, "north_latitude": 84, "east_longitude": 102}, "id": {"authority": "EPSG", "code": 32647}}
Study area CRS: EPSG:32647
0:34
Write
/work/solve.py
content · 66 lines · py
import geopandas as gpd
# Load data
landcover = gpd.read_parquet('bangkok_landcover.parquet')
study_area = gpd.read_file('bma_study_area.geojson')
# Ensure CRS match
if landcover.crs != study_area.crs:
landcover = landcover.to_crs(study_area.crs)
# Intersect landcover with study area
print("Intersecting...")
intersected = gpd.overlay(landcover, study_area, how='intersection')
# Calculate area in square meters
print("Calculating area...")
intersected['area_m2'] = intersected.geometry.area
# Select only necessary columns: 'class' and 'area_m2'
# We must preserve 'geometry'
columns_to_keep = ['class', 'area_m2', 'geometry']
# Some datasets might have different naming, check if 'class' exists
if 'class' not in intersected.columns:
print(f"Warning: 'class' column not found. Available: {intersected.columns.tolist()}")
# If user said 'class', let's look for anything similar or just print columns
else:
intersected = intersected[columns_to_keep]
# Simplify geometries to keep file size small
# Since CRS is UTM (meters), a tolerance of 1-5 meters should be fine for a preview.
print("Simplifying...")
# Use a small tolerance for simplification
intersected['geometry'] = intersected.geometry.simplify(tolerance=1.0, preserve_topology=True)
# Requirement: "every feature stored as a MultiPolygon"
print("Converting to MultiPolygon...")
intersected['geometry'] = intersected.geometry.apply(lambda geom:
gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom
)
# Wait, the above is a bit hacky. Let's do it properly.
# shapely.geometry.MultiPolygon can be constructed from a list of polygons.
from shapely.geometry import MultiPolygon, Polygon
def ensure_multipolygon(geom):
if geom.geom_type == 'Polygon':
return MultiPolygon([geom])
return geom
intersected['geometry'] = intersected.geometry.apply(ensure_multipolygon)
# Convert to WGS84 for GeoJSON standard (usually preferred for browsers, though not strictly required)
# The prompt didn't specify CRS for output, but GeoJSON is typically EPSG:4326.
# However, the user asked for 'area_m2' based on the intersection.
# If I convert to 4326, the geometry coordinates change.
# Let's check if the user wants the output in the metric CRS or 4326.
# Usually, GeoJSON files are 4326.
print("Reprojecting to WGS84 for GeoJSON output...")
intersected_wgs84 = intersected.to_crs(epsg=4326)
# Save to GeoJSON
print("Saving to bma_landcover_intersect.geojson...")
intersected_wgs84.to_file('bma_landcover_intersect.geojson', driver='GeoJSON')
print("Done.")
Wrote /work/solve.py (2586 chars).
0:35
Bash
python solve.py
Intersecting... Calculating area... Simplifying... Converting to MultiPolygon... Reprojecting to WGS84 for GeoJSON output... Saving to bma_landcover_intersect.geojson... Done.
/work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union [... truncated ...]instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom /work/solve.py:38: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead. gpd.GeoSeries([geom]).buffer(0).unary_union if geom.geom_type == 'Polygon' else geom
0:41
Bash
ls -lh bma_landcover_intersect.geojson
-rw-r--r-- 1 agent agent 4.9M Jun 7 15:48 bma_landcover_intersect.geojson
0:52
Write
/work/solve.py
content · 59 lines · py
import geopandas as gpd
from shapely.geometry import MultiPolygon
# Load data
landcover = gpd.read_parquet('bangkok_landcover.parquet')
study_area = gpd.read_file('bma_study_area.geojson')
# Ensure CRS match
if landcover.crs != study_area.crs:
landcover = landcover.to_crs(study_area.crs)
# Intersect landcover with study area
print("Intersecting...")
intersected = gpd.overlay(landcover, study_area, how='intersection')
# Calculate area in square meters
print("Calculating area...")
intersected['area_m2'] = intersected.geometry.area
# Select only necessary columns: 'class' and 'area_m2'
# We must preserve 'geometry'
columns_to_keep = ['class', 'area_m2', 'geometry']
if 'class' not in intersected.columns:
print(f"Warning: 'class' column not found. Available: {intersected.columns.tolist()}")
else:
intersected = intersected[columns_to_keep]
# Simplify geometries to keep file size small
# We use a tolerance of 2 meters for simplification in UTM
print("Simplifying...")
intersected['geometry'] = intersected.geometry.simplify(tolerance=2.0, preserve_topology=True)
# Ensure every feature is a MultiPolygon
print("Converting to MultiPolygon...")
def ensure_multipolygon(geom):
if geom.is_empty:
return None
if geom.geom_type == 'Polygon':
return MultiPolygon([geom])
elif geom.geom_type == 'MultiPolygon':
return geom
else:
# For other types (LineString, Point), we try to convert or discard
# But overlay should return Polygons/MultiPolygons
return None
intersected['geometry'] = intersected.geometry.apply(ensure_multipolygon)
intersected = intersected.dropna(subset=['geometry'])
# Convert to WGS84 for GeoJSON output
print("Reprojecting to WGS84 for GeoJSON output...")
intersected_wgs84 = intersected.to_crs(epsg=4326)
# Save to GeoJSON
print("Saving to bma_landcover_intersect.geojson...")
intersected_wgs84.to_file('bma_landcover_intersect.geojson', driver='GeoJSON')
print("Done.")
Wrote /work/solve.py (1981 chars).
0:57
Bash
python solve.py
Intersecting... Calculating area... Simplifying... Converting to MultiPolygon... Reprojecting to WGS84 for GeoJSON output... Saving to bma_landcover_intersect.geojson... Done.
1:06
Bash
ls -lh bma_landcover_intersect.geojson
-rw-r--r-- 1 agent agent 4.9M Jun 7 15:49 bma_landcover_intersect.geojson
1:17
Bash
python -c "import geopandas as gpd; gdf = gpd.read_file('bma_landcover_intersect...
Geometry types: <ArrowStringArray> ['MultiPolygon'] Length: 1, dtype: str Columns: ['class', 'area_m2', 'geometry']
1:20
Bash
python -c "import geopandas as gpd; gdf = gpd.read_file('bma_landcover_intersect...
class area_m2 geometry 0 forest 15735.747014 MULTIPOLYGON (((100.47947 13.57742, 100.47927 ... 1 forest 32598.532098 MULTIPOLYGON (((100.49889 13.59392, 100.49848 ... 2 barren 17000.227989 MULTIPOLYGON (((100.73264 13.64969, 100.73267 ... 3 crop 106792.699013 MULTIPOLYGON (((100.73906 13.75628, 100.73907 ... 4 forest 18200.655163 MULTIPOLYGON (((100.70272 13.63929, 100.703 13...
1:29