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.

Taras
  • 32,823
  • 4
  • 66
  • 137

2 Answers2

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

Taras
  • 32,823
  • 4
  • 66
  • 137
nr_aus
  • 3,535
  • 6
  • 26
  • 4
    Loading 5000 GeoJSON files into a QGIS project may cause QGIS to crash; speaking from personal experience. – nmtoken Mar 08 '24 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

BERA
  • 72,339
  • 13
  • 72
  • 161
  • 1
    One could also use the QgsVectorFileWriter with the writeAsVectorFormatV3() method for writing vector layers to disk based formats, see https://gis.stackexchange.com/questions/127749/exporting-layer-to-shapefile-using-pyqgis – Taras Mar 08 '24 at 08:40