4

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.

6
  • 2
    Do you have any python ability? This would be easy with os.listdir and ogr2ogr, skip QGIS altogether. Commented Mar 8 at 2:30
  • 1
    If you are able to use python, you can also consider geopandas
    – eurojam
    Commented Mar 8 at 5:39
  • 2
    Does "5000 different layers" mean that they have also different attribute schemas? Do you want to get 5000 different shapefiles as a result?
    – user30184
    Commented Mar 8 at 7:04
  • 1
    Are 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.
    – Joooeey
    Commented Mar 8 at 15:54
  • 1
    Qgis is probably the wrong tool. Use ogr2ogr. That's what Qgis is using under the covers.
    – Llaves
    Commented Mar 9 at 2:46

2 Answers 2

7

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

1
  • 4
    Loading 5000 GeoJSON files into a QGIS project may cause QGIS to crash; speaking from personal experience.
    – nmtoken
    Commented Mar 8 at 11:10
6

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

enter image description here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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