fio-l2-capetown-landuse-dissolve
Map (reference ← swipe → agent)
0:00
I'm prepping a transit-corridor study for the Cape Town metro, and I need a class-level summary of `landuse` so the team's spatial-SQL notebooks can join it against the bus-route table. Can you give me one row per landuse class with the geometry unified into a single MultiPolygon, the total area in m², and the count of source parcels that fed in? Please write the result to `landuse_dissolved.geoparquet` with `class`, `area_m2`, and `parcel_count` columns.
0:00
System info
{
"adapter": "openrouter",
"model": "google/gemma-4-26b-a4b-it",
"base_url": "https://openrouter.ai/api/v1"
}
0:04
Bash
ls -F
capetown_landuse.fgb
0:18
Write
/work/inspect_data.py
content · 27 lines · py
import geopandas as gpd
# Load the landuse data
gdf = gpd.read_file('capetown_landuse.fgb')
# Check the columns and data
print("Columns:", gdf.columns)
print(gdf.head())
print("CRS:", gdf.crs)
# Ensure we are in a projected CRS for area calculations (m2)
# Cape Town is typically in Lo29 (EPSG:32734 or similar) or WGS84.
# Let's check what we have and reproject to a metric CRS if it's geographic.
if gdf.crs.is_geographic:
print("Reprojecting to EPSG:32734 (UTM zone 34S) for area calculation...")
gdf = gdf.to_crs(epsg=32734)
else:
print("Already in a projected CRS.")
# Group by landuse class
# Assuming the column name for landuse is 'class' or similar.
# Let's find the correct column name from the print output.
# Based on the prompt, the user says "landuse class" and wants "class" column in result.
# I'll look for a column that looks like landuse.
# Let's check the column names again in the script logic.
Wrote /work/inspect_data.py (934 chars).
0:21
Bash
python /work/inspect_data.py
Columns: Index(['id', 'class', 'subtype', 'geometry'], dtype='str')
id ... geometry
0 f328c054-b040-3a7d-a015-53747d608fef ... MULTIPOLYGON (((872000.605 6252622.417, 871205...
1 f1943847-f785-390c-abb0-edc923f97997 ... MULTIPOLYGON (((253543.223 6283900.241, 254921...
2 c37e40aa-f202-3b15-b68d-89dafeeaa478 ... MULTIPOLYGON (((255047.308 6280773.284, 255024...
3 b9d39db7-5dcc-379e-bfeb-c994628df5a7 ... MULTIPOLYGON (((254978.723 6280842.179, 254992...
4 0033302c-c1d6-3624-b71e-0e300db64963 ... MULTIPOLYGON (((264759.63 6280552.85, 264825.5...
[5 rows x 4 columns]
CRS: EPSG:32734
Already in a projected CRS.
0:40
Write
/work/solve.py
content · 40 lines · py
import geopandas as gpd
# Load the landuse data
gdf = gpd.read_file('capetown_landuse.fgb')
# The inspection showed the CRS is already EPSG:32734 (projected)
# Columns: 'id', 'class', 'subtype', 'geometry'
# Group by 'class'
# 1. Geometry unified into a single MultiPolygon (unary_union)
# 2. Total area in m2
# 3. Count of source parcels (which is the count of rows per class)
# First, calculate area for each parcel
gdf['area_m2'] = gdf.geometry.area
# Aggregate
# We can use groupby and aggregate
# For 'geometry', we'll use unary_union
# For 'area_m2', we'll use sum
# For 'class', we'll use count (but we're grouping by it, so we want the count of the ids or just size)
summary = gdf.groupby('class').agg(
area_m2=('area_m2', 'sum'),
parcel_count=('id', 'count'),
geometry=('geometry', lambda x: x.unary_union)
).reset_index()
# Ensure the result column is 'class' as requested
# (It already is because it's the index)
# Check column order and types
print(summary.head())
print(summary.columns)
# Save to GeoParquet
summary.to_parquet('landuse_dissolved.geoparquet')
print("Successfully saved to landuse_dissolved.geoparquet")
Wrote /work/solve.py (1154 chars).
0:46
Bash
python /work/solve.py
class ... geometry 0 airfield ... POLYGON ((267829.758 6244743.665, 267820.919 6... 1 allotments ... MULTIPOLYGON (((263870.512 6232477.907, 263865... 2 animal_keeping ... MULTIPOLYGON (((264114.249 6261265.896, 264220... 3 barracks ... MULTIPOLYGON (((262288.681 6215543.331, 262280... 4 base ... POLYGON ((272069.73 6245748.538, 272064.541 62... [5 rows x 4 columns] Index(['class', 'area_m2', 'parcel_count', 'geometry'], dtype='str')
/work/solve.py:26: DeprecationWarning: The 'unary_union' attribute is deprecated, use the 'union_all()' method instead.
geometry=('geometry', lambda x: x.unary_union)
Traceback (most recent call last):
File "/work/solve.py", line 37, in <module>
summary.to_parquet('landuse_dissolved.geoparquet')
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.14/site-packages/pandas/core/frame.py", line 3135, in to_parquet
return to_parquet(
self,
...<7 lines>...
**kwargs,
)
File "/usr/local/lib/python3.14/site-packages/pandas/io/parquet.py", line 490, in to_parquet
impl.write(
~~~~~~~~~~^
df,
^^^
...<6 lines>...
**kwargs,
^^^^^^^^^
)
^
File "/usr/local/lib/python3.14/site-packages/pandas/io/parquet.py", line 191, in write
table = self.api.Table.from_pandas(df, **from_pandas_kwargs)
File "pyarrow/table.pxi", line 4768, in pyarrow.lib.Table.from_pandas
File "/usr/local/lib/python3.14/site-packages/pyarrow/pandas_compat.py", line 651, in dataframe_to_arrays
arrays = [convert_column(c, f)
~~~~~~~~~~~~~~^^^^^^
File "/usr/local/lib/python3.14/site-packages/pyarrow/pandas_compat.py", line 639, in convert_column
raise e
File "/usr/local/lib/python3.14/site-packages/pyarrow/pandas_compat.py", line 633, in convert_column
result = pa.array(col, type=type_, from_pandas=True, safe=safe)
File "pyarrow/array.pxi", line 390, in pyarrow.lib.array
File "pyarrow/array.pxi", line 87, in pyarrow.lib._ndarray_to_array
shared_ptr[CDataType] c_type = _ndarray_to_type(values, type)
File "pyarrow/array.pxi", line 75, in pyarrow.lib._ndarray_to_type
c_type = GetResultValue(NumPyDtypeToArrow(dtype))
File "pyarrow/error.pxi", line 155, in pyarrow.lib.pyarrow_internal_check_status
File "pyarrow/error.pxi", line 92, in pyarrow.lib.check_status
raise convert_status(status)
pyarrow.lib.ArrowTypeError: ('Did not pass numpy.dtype object', 'Conversion failed for column geometry with type geometry')
1:07
Write
/work/solve.py
content · 48 lines · py
import geopandas as gpd
# Load the landuse data
gdf = gpd.read_file('capetown_landuse.fgb')
# The inspection showed the CRS is already EPSG:32734 (projected)
# Columns: 'id', 'class', 'subtype', 'geometry'
# First, calculate area for each parcel
gdf['area_m2'] = gdf.geometry.area
# Group by 'class'
# We'll use union_all() if available, or unary_union
# To ensure it is a GeoDataFrame, we should convert the result back to a GeoDataFrame
# We'll aggregate manually to avoid the pandas/pyarrow issue with direct conversion
# of aggregated results that might lose the GeoSeries property.
summary_list = []
for landuse_class, group in gdf.groupby('class'):
# Sum area
total_area = group['area_m2'].sum()
# Count parcels
count = len(group)
# Dissolve geometry
dissolved_geom = group.geometry.union_all() if hasattr(group.geometry, 'union_all') else group.geometry.unary_union
summary_list.append({
'class': landuse_class,
'area_m2': total_area,
'parcel_count': count,
'geometry': dissolved_geom
})
# Create GeoDataFrame from the list of dicts
summary_gdf = gpd.GeoDataFrame(summary_list, crs=gdf.crs)
# Reorder columns to match requested
summary_gdf = summary_gdf[['class', 'area_m2', 'parcel_count', 'geometry']]
print(summary_gdf.head())
print(summary_gdf.columns)
# Save to GeoParquet
summary_gdf.to_parquet('landuse_dissolved.geoparquet')
print("Successfully saved to landuse_dissolved.geoparquet")
Wrote /work/solve.py (1480 chars).
1:11
Bash
python /work/solve.py
class ... geometry 0 airfield ... POLYGON ((267829.758 6244743.665, 267820.919 6... 1 allotments ... MULTIPOLYGON (((263870.512 6232477.907, 263865... 2 animal_keeping ... MULTIPOLYGON (((264114.249 6261265.896, 264220... 3 barracks ... MULTIPOLYGON (((262288.681 6215543.331, 262280... 4 base ... POLYGON ((272069.73 6245748.538, 272064.541 62... [5 rows x 4 columns] Index(['class', 'area_m2', 'parcel_count', 'geometry'], dtype='str') Successfully saved to landuse_dissolved.geoparquet
1:40