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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












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)?











share|improve this question







New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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
    2 hours ago















up vote
2
down vote

favorite












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)?











share|improve this question







New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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
    2 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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)?











share|improve this question







New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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)?








python json geopandas fiona geopackage






share|improve this question







New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









Aaron Bramson

1134




1134




New contributor




Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Aaron Bramson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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
    2 hours ago

















  • 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
    2 hours ago
















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
2 hours ago





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
2 hours ago











1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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.






share|improve this answer




















  • 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
    1 hour ago










  • 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
    9 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Aaron Bramson is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f298530%2fhow-do-i-write-a-geopandas-dataframe-into-a-single-file-preferably-json-or-geop%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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.






share|improve this answer




















  • 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
    1 hour ago










  • 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
    9 mins ago














up vote
2
down vote



accepted










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.






share|improve this answer




















  • 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
    1 hour ago










  • 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
    9 mins ago












up vote
2
down vote



accepted







up vote
2
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









toms

5,13411220




5,13411220











  • 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
    1 hour ago










  • 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
    9 mins ago
















  • 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
    1 hour ago










  • 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
    9 mins ago















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
1 hour ago




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
1 hour ago












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
9 mins ago




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
9 mins ago










Aaron Bramson is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















Aaron Bramson is a new contributor. Be nice, and check out our Code of Conduct.












Aaron Bramson is a new contributor. Be nice, and check out our Code of Conduct.











Aaron Bramson is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f298530%2fhow-do-i-write-a-geopandas-dataframe-into-a-single-file-preferably-json-or-geop%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke