10

I'm a beginner at programming and get pretty confused when trying to use external libraries. I have my maps in shapefiles that I convert to GeoJSON using the website Mapshaper.org, and only then can i read the map from my Java application.

I want the user to be able to import a shapefile directly so I found out about GDAL ogr2ogr that has Java bindings, and can make this conversion with a single command such as:

$ ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp

but I have no idea how to use this from my program.

Can someone provide me with an example of how to implement this? How should I import GDAL to my project? I'm using LibGDX if that's of any help.

In the end, all i need is a way to convert shapefile to GeoJSON directly on my application so the user can add new maps himself, so any suggestion is welcome.

Thank you so much!

user29903
  • 101
  • 1
  • 3

1 Answers1

8

First you need to install the Java Bindings (see here at the bottom). Than download the ogr2ogr.java from here (scroll down -> download original format -> put it into your project folder). Than it gets quite easy. You just cann call ogr2ogr.java's main method. ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp :

String[] cmd = {"-t_srs", "CRS:84", "-f", "GeoJSON","[name].geojson","[name].shp"};

ogr2ogr.main(cmd);
Selphiron
  • 897
  • 1
  • 12
  • 30
  • On the page [Build Instructions for GDAL/OGR In Java (Windows)](https://trac.osgeo.org/gdal/wiki/GdalOgrInJavaBuildInstructions) it is written _Note that ​OSGeo4W includes a ​gdal-java package and a Java runtime making it fairly easily to build local Java applications using GDAL._ but I did not find any gdal-java package or any jni library. – Deepak Pandey Jun 07 '18 at 09:59