I am aware that I can individually load my GeoJSON layer and save it as a ESRI Shapefile. I am wondering if anyone is aware of a tool that can bulk load and convert my 5,000 different GeoJSON layers into an ESRI Shapefile. It doesn't have to be all at once. But if there was a way to do a few hundred at a time, that would be helpful.
-
2Do you have any python ability? This would be easy with os.listdir and ogr2ogr, skip QGIS altogether.– Michael StimsonCommented Mar 8 at 2:30
-
1If you are able to use python, you can also consider geopandas– eurojamCommented Mar 8 at 5:39
-
2Does "5000 different layers" mean that they have also different attribute schemas? Do you want to get 5000 different shapefiles as a result?– user30184Commented Mar 8 at 7:04
-
1Are you confident that your GeoJSONs can be translated to Shapefiles? The GeoJSON data model is significantly more flexible than Shapefile. E.g. you can have column names with more than 10 characters, mix different geometry types, et cetera in GeoJSON but not in Shapefile.– JoooeeyCommented Mar 8 at 15:54
-
1Qgis is probably the wrong tool. Use ogr2ogr. That's what Qgis is using under the covers.– LlavesCommented Mar 9 at 2:46
2 Answers
In QGIS some of the Geoprocessing tools have a Batch Processing option, see the official QGIS documentation for more details.
There are a couple of tools that you could try, namely the "Save Vector Features to file".
In the bottom left of the GUI is the Run as Batch Processing... button.
Then you just add in all your GeoJSON files into the project, import them into the batch processing, select a single .shp as the output. You will probably need to put an option command in the GDAL Layer Options box, -append
if I remember correctly.
The only condition would be that your GeoJSON files would need to be of the same geometry type and the attributes would need to be the same. If they aren't, you will get funny results (e.g.: Trying to save a line geometry and polygon geometry into a single shapefile won't work).
-
4Loading 5000 GeoJSON files into a QGIS project may cause QGIS to crash; speaking from personal experience.– nmtokenCommented Mar 8 at 11:10
One option is to use PyQGIS:
import os
output_shape_folder = r"C:\GIS\GIStest\shapefiles" #Folder to save the shape-converted geojsons in
#List all geojson files
geojson_folder = r"C:\GIS\GIStest" #All geojson files in this folder and all subfolders in it will be listed for conversion to shape
geojson_file_list = [] #An empty list to hold all geojson filenames
for root, folder, files in os.walk(geojson_folder):
for file in files:
if file.endswith(".geojson"): #If it is a geojson
fullname = os.path.join(root, file)
geojson_file_list.append(fullname) #Append the full filename to the list
#Convert each geojson to shape
geojson_subset = geojson_file_list[0:10] #Slice the geojson list so they arent all converted at once. I'm slicing the first 10 files. #https://www.learnbyexample.org/python-list-slicing/
for geojson_file in geojson_subset: #For each geojson in the subset list
print(f"Converting {geojson_file}")
out_shape_file = os.path.join(output_shape_folder, os.path.basename(geojson_file).replace(".geojson", ".shp"))
processing.run("gdal:convertformat", {'INPUT':geojson_file,'CONVERT_ALL_LAYERS':False,
'OPTIONS':'','OUTPUT':out_shape_file})
-
1One could also use the
QgsVectorFileWriter
with thewriteAsVectorFormatV3()
method for writing vector layers to disk based formats, see gis.stackexchange.com/questions/127749/…– Taras ♦Commented Mar 8 at 8:40