4

I'm new to GIS and having a basic beginner problem. I've got data in the form of a geopackage .gpkg from GADM.org. I can read in and use the data without problem using Geopandas (via built-in Fiona).

I've added and deleted some columns to the data, and changed some column names to ones more intuitive to me (for ease of later use and consistency across files). Minor edits.

Now I want to export the data to a file format that is easy to later read into Python as a GeoPandas dataframe. However, whenever I try

CityShapeFile.to_file('CityShapeFile_edited.gpkg',encoding='utf-8')

No matter which format I choose (e.g. gpkg, json, shp, ...) I don't get a single file, but rather a folder with 5 files: *.cpg, *.dbf, *.prj, *.shp, and *.shx. I'm sure those files together contain all the data I need, but in order to read the data back into Python I really need it in one file (as far as I know). Considering the original data I downloaded is in a single gpkg file, this seems possible and probably normal, but how to do it?

I read in the Fiona manual that it can write zipped shapefiles, but I couldn't find any simple example of doing that with a GeoPandas dataframe, nor am I sure whether that can be read in correctly. So,

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

  • You can load a shapefile as a geodataframe, just read the .shp file. Check out the documentation for writing: geopandas.org/reference.html, there is a to_json() method that will write your gdf to a single json file. I believe you can add driver='GPKG' to you to_file() call to export a gpkg. – Jon Oct 11 '18 at 3:58
6

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.

  • This worked perfectly. I had no idea, and saw no previous examples, of specifying the driver when writing the file. On reflection I would expect that the packages would be clever enough to determine the driver from the file extension, but at least I now know the trick. – Aaron Bramson Oct 11 '18 at 5:09
  • The documentation is indeed a bit sparse .. Feel free to open an issue for that on github.com/geopandas/geopandas, or do a pull request to update! – joris Oct 11 '18 at 6:15
0

@toms' answer is perfect -- which I've used for creating my geopackage, btw. Now that I've learned, I want to add to the discussion:

GeoDataFrame's to_file method has the following signature:

>>> help(dataframe.to_file)

to_file(filename, driver='ESRI Shapefile', schema=None, **kwargs)
...

And it follows:

...
A dictionary of supported OGR
    providers is available via:

    >>> import fiona
    >>> fiona.supported_drivers

As of fiona.__version__ == 1.8.6 those are the supported drivers:

{'AeronavFAA': 'r',
 'ARCGEN': 'r',
 'BNA': 'raw',
 'DXF': 'raw',
 'CSV': 'raw',
 'OpenFileGDB': 'r',
 'ESRIJSON': 'r',
 'ESRI Shapefile': 'raw',
 'GeoJSON': 'rw',
 'GPKG': 'rw',
 'GML': 'raw',
 'GPX': 'raw',
 'GPSTrackMaker': 'raw',
 'Idrisi': 'r',
 'MapInfo File': 'raw',
 'DGN': 'raw',
 'S57': 'r',
 'SEGY': 'r',
 'SUA': 'r',
 'TopoJSON': 'r'}

Which is a pretty interesting list, btw. It is just not clear the relation between file-formats extension/name, which may look a bit "noisy" for newcomers (like me), but a second round of googles will do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.