I will explain with an example:

I have 2 shp files for Tanzania.

Tanzania inland waters http://www.diva-gis.org/data/wat/TZA_wat.zip

Tanzania administrative boundaries http://gadm.org/data/shp/TZA_adm.zip

I open both shp files (TZA_wat.shp and TZA_adm0.shp) in Quantum GIS Desktop. I want to merge the layers : TZA_wat and TZA_adm0 and export/save both layers to one geojson file.

While I can do this for individual layers but not able to do this for both at once. I have written code to process geojson files but I get stuck at this step.

Is there a way to first merge both these .shp files to a single .shp file and then export/save as geojson?

I used the Union option in QGIS and merged the two shp files (here) into one. I uploaded the file here in a zip if anyone wants to take a look. The zip has shp files and geojson file in it.

input shp files : link output shp file: link


The problem with your data is that you have one line type layer, and the rest are polygon layers. You can merge data of one type, but not mixed (at least not with shapefiles).

So you have to do it by editing the geojson file, or leave it separated type-wise. QGIS is not able to work with mixed geometries, so you have to split a mixed-geometry input file anyway (like gpx or kml drivers do).


Improved answer with new data:

Now you have only polgon data, which makes a union possible without mixed geometry. But your admin shapefile has no prj file. That might cause the crash, see http://hub.qgis.org/issues/5962. Union does it better, but makes 2239 objects out of 83 admin area objects and 892 water areas.

What you can do is clear the union file, and copy and paste the elements of the two layers again into the output layer. The union file still has all attribute fields from both datasets, so copying works. In result you only have 975 elements.

Or copy and rename the .prj file from the water areas, which has the same projection. Then merging shapefiles works, with the expected 975 elements.

  • Vector -> Geoprocessing Tools -> Union worked for me. – Mr.Hunt Feb 5 '13 at 11:34
  • Are you sure you have the line geometry of the rivers in the polygon union? It does not give an error message. You can transform the polygons to lines as well, but you might want to work on with polygons. – AndreJ Feb 5 '13 at 11:40
  • I updated my question & added a link to file I generated. You can take a look. – Mr.Hunt Feb 5 '13 at 12:16
  • Rather strange: You merged 26 admin elements and 892 water_areas and got 2239 elements in the union file. But the 6903 elements of TZA_water_lines_dcw are still missing. I think simple copy and paste from one shapefile to the other would be much more simple. Apart from that, the admin borders are not in line. – AndreJ Feb 5 '13 at 12:31
  • Sorry, I used different input files than the one in example. I have attached the link to input files used and output generated. HTH. – Mr.Hunt Feb 5 '13 at 14:29

To merge the files, if there are just two you can use Vector->Geoprocessing Tools->Union. Alternatively you can use Vector->Data Management->Merge Shapefiles to one.

Once you have your shapefiles merged simply open the new shapefile in QGIS and save it as GeoJSON.

  • Vector -> Geoprocessing Tools -> Union worked for me. When I tried to use Merge Shapefiles option QGIS kept crashing. One thing I noticed that the output file size was much larger (5mb) compared to input files (both less than 1mb). – Mr.Hunt Feb 5 '13 at 11:27

As json is plain text you can even use a text editor: Export both to geojson and just paste everything between the "[]" at "features" variable to the "features" in another file with a geojson structure, this looks like this:

"type": "FeatureCollection",                                                                              
"features": [ **paste here** ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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