如何在Java中运行GDAL(ogr2ogr)将Shapefile转换为GeoJSON [英] How to run GDAL (ogr2ogr) in Java to convert Shapefiles to GeoJSON

查看:333
本文介绍了如何在Java中运行GDAL(ogr2ogr)将Shapefile转换为GeoJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,在尝试使用外部库时会非常困惑。我的shapefile中的地图使用Mapshaper.org网站转换为GeoJSON,然后才能从我的Java应用程序中读取地图。

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.

我想要用户能够直接导入shapefile,所以我发现有 ogr2ogr http://trac.osgeo.org/gdal/wiki/GdalOgrInJava =noreferrer> Java绑定,可以使用单个命令进行此转换,例如:

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.

有人能为我提供一个如何实现这个的例子吗?我该如何将GDAL导入我的项目?如果有任何帮助,我正在使用LibGDX。

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.

最后,我需要的是一种直接在我的应用程序上将shapefile转换为GeoJSON的方法,以便用户可以添加新地图本身,所以任何建议都是受欢迎的。

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.

非常感谢你!

推荐答案

首先你需要安装Java Bindings(参见底部的这里) )。而不是从这里(向下滚动 - >下载原始格式 - >将其放入项目文件夹中)。
比它变得容易。你只需要调用ogr2​​ogr.java的主要方法。 ogr2ogr -f GeoJSON -t_srs crs:84 [name] .geojson [name] .shp

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

这篇关于如何在Java中运行GDAL(ogr2ogr)将Shapefile转换为GeoJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆