Vector geometry toolkit¶
Build and reshape FeatureCollections:
from_bbox/from_records— construct collections from a box or from dict rows.with_coordinates/with_centroid— attach x/y or centroid columns.explode— split multi-part geometries into single parts.concat— stack two collections.
Setup¶
In [1]:
Copied!
%matplotlib inline
import tempfile
from pathlib import Path
import numpy as np
DATA = Path('../../../examples/data')
WORK = Path(tempfile.mkdtemp(prefix='pyramids-t3-'))
DATA.is_dir(), WORK.is_dir()
%matplotlib inline
import tempfile
from pathlib import Path
import numpy as np
DATA = Path('../../../examples/data')
WORK = Path(tempfile.mkdtemp(prefix='pyramids-t3-'))
DATA.is_dir(), WORK.is_dir()
Out[1]:
(True, True)
In [2]:
Copied!
from shapely.geometry import Point
from pyramids.feature import FeatureCollection
from shapely.geometry import Point
from pyramids.feature import FeatureCollection
2026-07-11 14:42:28 | INFO | pyramids.base.config | Logging is configured.
Construct — from_bbox and from_records¶
In [3]:
Copied!
box = FeatureCollection.from_bbox((0.0, 0.0, 1.0, 1.0), epsg=4326)
pts = FeatureCollection.from_records(
[{'id': 1, 'geometry': Point(0, 0)}, {'id': 2, 'geometry': Point(1, 1)}], crs=4326
)
(len(box), box.epsg), (len(pts), list(pts.columns))
box = FeatureCollection.from_bbox((0.0, 0.0, 1.0, 1.0), epsg=4326)
pts = FeatureCollection.from_records(
[{'id': 1, 'geometry': Point(0, 0)}, {'id': 2, 'geometry': Point(1, 1)}], crs=4326
)
(len(box), box.epsg), (len(pts), list(pts.columns))
Out[3]:
((1, 4326), (2, ['id', 'geometry']))
Derive columns — with_coordinates / with_centroid¶
In [4]:
Copied!
polys = FeatureCollection.read_file(DATA / 'coello_polygons.geojson')
coords = polys.with_coordinates()
centroids = polys.with_centroid()
[c for c in coords.columns if c in ('x', 'y')], type(centroids).__name__
polys = FeatureCollection.read_file(DATA / 'coello_polygons.geojson')
coords = polys.with_coordinates()
centroids = polys.with_centroid()
[c for c in coords.columns if c in ('x', 'y')], type(centroids).__name__
Out[4]:
(['x', 'y'], 'FeatureCollection')
Plot the source polygons, then their centroids — the same features reduced to a single representative point each.
In [5]:
Copied!
polys.plot()
centroids.plot()
polys.plot()
centroids.plot()
Out[5]:
<Axes: >
Reshape — explode and concat¶
In [6]:
Copied!
single_parts = polys.explode()
stacked = polys.concat(polys)
(len(polys), '-> explode ->', len(single_parts)), ('concat ->', len(stacked))
single_parts = polys.explode()
stacked = polys.concat(polys)
(len(polys), '-> explode ->', len(single_parts)), ('concat ->', len(stacked))
Out[6]:
((4, '-> explode ->', 4), ('concat ->', 8))
Plot the exploded single-part geometries — each multi-part feature is now split into its own row.
In [7]:
Copied!
single_parts.plot()
single_parts.plot()
Out[7]:
<Axes: >
Notes¶
- A
FeatureCollectionis ageopandas.GeoDataFramesubclass, so all of geopandas works too. - See also: Rasterize ↔ vectorize, Vector formats.