Skip to main content
46 votes
Accepted

How do I write a GeoPandas dataframe into a single file (preferably JSON or GeoPackage)?

To write to GeoJSON: dataframe.to_file("output.json", driver="GeoJSON") To write to GeoPackage: dataframe.to_file("output.gpkg", driver="GPKG") Documentation is here, though somewhat sparse.
toms's user avatar
  • 7,008
36 votes
Accepted

Reading the names of geodatabase file layers in Python

The fiona.listlayers() function returns a list of names of layers in a dataset. import fiona fiona.listlayers('NYCFutureHighTideWithSLR.gdb') Any of the elements of the list can be used as a value ...
sgillies's user avatar
  • 9,046
25 votes
Accepted

Drivers of Fiona

you can get a list of the drivers with >>> import fiona >>> fiona.supported_drivers {'ESRI Shapefile': 'raw', 'ARCGEN': 'r', 'PCIDSK': 'r', 'SUA': 'r', 'DGN': 'raw', 'SEGY': 'r', '...
nickves's user avatar
  • 11.5k
22 votes
Accepted

Reading raw data into geopandas

You can pass the json directly to the GeoDataFrame constructor: import geopandas as gpd import requests data = requests.get("https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&...
user2856's user avatar
  • 66.3k
19 votes
Accepted

Intersecting two shape problem using geopandas

1) The problem is that the intersection of two polygons is not always a polygon import geopandas as gp poly1 = gp.read_file("poly_origin.shp") poly2 = gp.read_file("poly_test.shp") data = [] for ...
gene's user avatar
  • 55k
18 votes
Accepted

GeoPandas read_file only reading first part of KML

Document tags in KML file are separate layers for GeoPandas. When you try to get the KML content, you actually get the first layer. So you need for loop for iterating over layers. import requests ...
Kadir Şahbaz's user avatar
15 votes

Snap points shapefile to line shapefile using shapely

The methodology is called linear referencing and a solution was given by Mike T in Coordinate of the closest point on a line with Shapely. There is also a recipe in the Python Geospatial Analysis ...
gene's user avatar
  • 55k
14 votes
Accepted

Plot shapefile on top of raster using plot and imshow from matplotlib

I added that recipe to the rasterio documentation. Since it was such a simple shape, in this case I just unzipped the coords in the single record contained by the shapefile. That is, x, y = zip(*...
jdmcbr's user avatar
  • 904
14 votes
Accepted

GeoPandas to_file() saves GeoDataFrame without coordinate system

I have come across this behavior before. You need to explicitly pass the well known text (crs_wkt) string to the to_file() method. The string will then get passed to fiona.open(), which writes out ...
Nxaunxau's user avatar
  • 3,933
14 votes
Accepted

GeoPandas ValueError: A LinearRing must have at least 3 coordinate tuples

Figured it out after beating my head for a lot of time. Change this line gdf = gpd.read_file(layer_file) to #Read data collection = list(fiona.open(layer_file,'r')) df1 = pd.DataFrame(collection) ...
Sandeep Kumar's user avatar
13 votes

Rasterize a shapefile with Geopandas or fiona - python

geocube is a new tool specifically designed for rasterizing geopandas data that wraps rasterio. It simplifies the process and eliminates the need for a template raster. https://github.com/corteva/...
snowman2's user avatar
  • 7,419
13 votes
Accepted

Converting shapefile from projected coordinates using pyproj and fiona

At the first question, 'epsg:32054' code has feet units. For this reason, it is necessary to use 'preserve_units=True' as parameter in inProj = Proj(init='epsg:32054') line. Now, next code works well: ...
xunilk's user avatar
  • 29.9k
13 votes
Accepted

Computing convex hull of points using shapely

To transform the shapefile geometries to shapely geometries (you can't build a MultiPoligon with a single point as in your solution) from shapely.geometry import Point, shape import fiona for feat ...
gene's user avatar
  • 55k
11 votes
Accepted

How to add support for FileGDB (Esri file gdb API) driver in fiona?

The Gohlke GDAL/OGR wheel includes the FileGDB driver compiled as a plugin. To get the FileGDB driver working: Copy the Esri bin64\FileGDB.dll to [python install/virtualenv dir]\Lib\site-packages\...
user2856's user avatar
  • 66.3k
11 votes
Accepted

Listing every layer in Geopackage using Fiona

It's simple to do this using Fiona. I prefer using GeoPandas to read the Geopackage, and then Fiona to list the layers: import geopandas as gpd import fiona gpkg = 'path/to.gpkg' layers = fiona....
Encomium's user avatar
  • 3,143
10 votes
Accepted

Adding column to shapefile with Fiona?

It seems to me that you have some problems with Fiona. Remember that all the elements are dictionaries 1) you define a new field schema['properties']['pi'] = 'float' 2) this field must be in in the ...
gene's user avatar
  • 55k
10 votes
Accepted

Open GPKG embedded layers in python

Use the layer parameter: gdf = gpd.read_file(r'C:\folder\geopackage.gpkg', layer='somelayername') read_file uses fiona.open, see: fiona.open docs
BERA's user avatar
  • 73.7k
9 votes
Accepted

Geopandas Polygon to matplotlib patches Polygon conversion

There is a Python module for that: Descartes (look at Plot shapefile with matplotlib for example) from geopandas import GeoDataFrame test = GeoDataFrame.from_file('poly1.shp') test.set_index('id', ...
gene's user avatar
  • 55k
8 votes

Fiona throws no dataset found when attempting to read a KMZ file

Two steps: (1) enable KML drivers for fiona: fiona.drvsupport.supported_drivers['kml'] = 'rw' # enable KML support fiona.drvsupport.supported_drivers['KML'] = 'rw' (2) Unzip the KMZ file to find ...
David's user avatar
  • 303
8 votes
Accepted

How can I extract contours from a raster with Python?

I'd recommend using gdal_contour . The results are likely to be much better than any attempt to re-implement it :-) Having said that, there should be a way to do this in rasterio. I've not tried ...
Steven Kay's user avatar
  • 20.4k
8 votes
Accepted

How to get all records if WFS output limited to 1000?

Check the service capabilities https://geoservices.noord-holland.nl/ags/services/oi_dataservice_alg/MapServer/WFSServer?request=GetCapabilities&service=WFS&version=2.0.0 <ows:Constraint ...
user30184's user avatar
  • 66.2k
7 votes

How to find the intersection areas of overlapping buffer zones in single shapefile?

The problem here is that you need to iterate over two polygons of the layer of polygons as you do when you iterate through two layers (look at intersecting two shapefiles from Python or command line) ...
gene's user avatar
  • 55k
7 votes
Accepted

Select by attribute using fiona

Much more easily: with fiona.open("a_shapefile.shp") as input: meta = input.meta with fiona.open('a_shapefile2.shp', 'w',**meta) as output: for feature in input: if ...
gene's user avatar
  • 55k
7 votes
Accepted

Find common borders of features and get their attributes using fiona and shapely

As Fiona works with dictionaries, it is easy. With the example of Finding the common borders between polygons in the same shapefile import fiona from shapely.geometry import shape, mapping # load ...
gene's user avatar
  • 55k
7 votes
Accepted

Only read specific rows of a shapefile with GeoPandas / Fiona

If you don't want to read the whole file, another solution is to use a generator as in Only read specific attribute columns of a shapefile with Geopandas / Fiona As a reminder (from What does the “...
gene's user avatar
  • 55k
7 votes

RTree spatial index does not result in faster intersection computation

My answer is essentially based on another answer by @gene here: More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc He proposed the same solution using two differents methods, ...
mgri's user avatar
  • 16.2k
7 votes

Geopandas: Write layer back into GeoDataBase

As with Fiona where you can specify the driver when you save a layer, you can do the same thing with GeoPandas (Writing Spatial Data) GeoDataFrames can be exported to many different standard ...
gene's user avatar
  • 55k
7 votes

How to write a multi geometries .geojson file with Shapely/Fiona?

import fiona import json shp = fiona.open(r"some_shapes.shp") geojson = { "type": "FeatureCollection", "features": [] } for f in shp: geojson["features"].append(f) with open(r"...
Andreas Müller's user avatar
6 votes

Reading/writing utf-8 encoded data using Fiona?

Fiona read ok utf-8 files without add nothing. Try encoding='utf-8' only when output with fiona. So change... with fiona.open(out_file, 'w', **meta) as output: To with fiona.open(out_file, 'w', ...
Juanma Font's user avatar
6 votes
Accepted

Only read specific attribute columns of a shapefile with Geopandas / Fiona

Update for this topic: geopandas & fiona now support reading specifiec columns from files using the ignore_fields or include_fields parameters in gpd.read_file(). https://geopandas.org/en/stable/...
Ofir's user avatar
  • 677

Only top scored, non community-wiki answers of a minimum length are eligible