5

I have a directory of GeoJSON files, which I want to load as layers into a single new GeoPackage file. According to the GDAL GeoPackage documentation, I can name my destination GeoPackage file, enter a directory of shapefiles as the source, and it will load them each as their own layer/table. Indeed, I've tested this with shapefiles, and it works.

When I try the same thing with GeoJSON files, however, I get this:

Unable to open datasource ./seattle_washington_osm with the following drivers.
   …
-> GeoJSON
   …

Is this simply not possible with GeoJSON files, or am I doing something wrong?

2 Answers 2

7

The "directory as dataset" is a specific feature of the shapefile driver. From http://www.gdal.org/drv_shapefile.html :

Normally the OGR Shapefile driver treats a whole directory of shapefiles as a dataset, and a single shapefile within that directory as a layer. In this case the directory name should be used as the dataset name. However, it is also possible to use one of the files (.shp, .shx or .dbf) in a shapefile set as the dataset name, and then it will be treated as a dataset with one layer.

Alternative approaches would be to load each file using a loop, or to convert to shapefiles first.

3

I failed to convert a directory of GeoJSONs into one pgkg using ogr2ogr (GDAL 2.2.3) by using the command ogr2ogr -f GPKG filename.gpkg ./path/to/geojsons/*; But I succeed in using the -append parameter in ogr2ogr command to convert GeoJSON data into one pgkg one by one.

I will show my shell scripts here:

#! /bin/bash
function read_dir(){
for file in `ls $1` 
do
 res="${file:${#file}-7:7}"
 if [ "$res" == "geojson" ] ;then
  name="${file:0:${#file}-8}" 
  echo $name
  ogr2ogr -f GPKG filename.gpkg  -append $file
 fi
done
}
read_dir $1

At last I got the output file called filename.gpkg and it works well.

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.