Converting between vector formats¶
FeatureCollection.read_file reads any OGR vector format; to_file(path) writes by extension,
and to_parquet writes GeoParquet. CRS and attributes carry through.
| Target | Call | Notes |
|---|---|---|
| GeoJSON | to_file('.geojson') |
text, web-friendly |
| Shapefile | to_file('.shp') |
legacy, multi-file |
| GeoPackage | to_file('.gpkg') |
single-file SQLite |
| GeoParquet | to_parquet(...) |
columnar, fast ([parquet] extra) |
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-convert-')) # scratch dir for outputs
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-convert-')) # scratch dir for outputs
DATA.is_dir(), WORK.is_dir()
Out[1]:
(True, True)
In [2]:
Copied!
from pyramids.feature import FeatureCollection
fc = FeatureCollection.read_file(DATA / 'coello_polygons.geojson')
len(fc), fc.epsg
from pyramids.feature import FeatureCollection
fc = FeatureCollection.read_file(DATA / 'coello_polygons.geojson')
len(fc), fc.epsg
2026-07-11 14:37:32 | INFO | pyramids.base.config | Logging is configured.
Out[2]:
(4, 32618)
Plot the source polygons before converting — every format below stores these same four features.
In [3]:
Copied!
fc.plot()
fc.plot()
Out[3]:
<Axes: >
GeoJSON → Shapefile and GeoPackage¶
The extension selects the OGR driver.
In [4]:
Copied!
shp = WORK / 'polys.shp'
fc.to_file(shp)
gpkg = WORK / 'polys.gpkg'
fc.to_file(gpkg)
(
len(FeatureCollection.read_file(shp)),
len(FeatureCollection.read_file(gpkg)),
)
shp = WORK / 'polys.shp'
fc.to_file(shp)
gpkg = WORK / 'polys.gpkg'
fc.to_file(gpkg)
(
len(FeatureCollection.read_file(shp)),
len(FeatureCollection.read_file(gpkg)),
)
2026-07-11 14:37:33 | INFO | pyogrio._io | Created 4 records
2026-07-11 14:37:33 | INFO | pyogrio._io | Created 4 records
Out[4]:
(4, 4)
Read the GeoPackage back and plot it — the geometries and CRS carried through the conversion unchanged.
In [5]:
Copied!
FeatureCollection.read_file(gpkg).plot()
FeatureCollection.read_file(gpkg).plot()
Out[5]:
<Axes: >
GeoJSON → GeoParquet¶
GeoParquet is columnar and compresses well. It needs the [parquet] extra
(pip install 'pyramids-gis[parquet]'); the cell falls back to a note if it is missing.
In [6]:
Copied!
pq = WORK / 'polys.parquet'
try:
fc.to_parquet(pq)
outcome = ('wrote geoparquet', len(FeatureCollection.read_parquet(pq)))
except Exception as exc: # pyarrow / parquet extra not installed
outcome = ('parquet extra missing', type(exc).__name__)
outcome
pq = WORK / 'polys.parquet'
try:
fc.to_parquet(pq)
outcome = ('wrote geoparquet', len(FeatureCollection.read_parquet(pq)))
except Exception as exc: # pyarrow / parquet extra not installed
outcome = ('parquet extra missing', type(exc).__name__)
outcome
Out[6]:
('wrote geoparquet', 4)
Notes¶
to_fileinfers the OGR driver from the extension;to_parquetis the GeoParquet writer.- Shapefiles truncate field names to 10 chars and split across
.shp/.shx/.dbf/.prj— prefer GeoPackage or GeoParquet for new work. - See also: GeoTIFF ↔ NetCDF and Other raster formats.