1.业务需求
要求将前端的地图标绘(使用openLayer)内容存储到数据库,并能够展示及下载为shp文件,本人负责后端开发
2.思路
2.1 了解前端返回的geoJson,其格式一般如下
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}
2.2 了解数据的入库及出库
经过网上查找资料及同事的答疑,初步确定以下方案
核心关注点是geoJson中的geometry对象入库,每个geometry对应数据库的一条记录,即也可以理解为每个feature对应一条数据,但就我自身的难点而言是对geometry对象的入库,以下即为一个geometry的json数据
{
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
}
对于该json数据,入库的操作步骤如下
- 转为可入库的数据类型 ----将json转为wkt类型的字符串,使用以下工具类
/**
*
*/
package com.htht.project.gdal;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import org.gdal.gdal.gdal;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Feature;
import org.gdal.ogr.FeatureDefn;
import org.gdal.ogr.FieldDefn;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.Layer;
import org.gdal.ogr.ogr;
import org.geotools.geojson.geom.GeometryJSON;
import com.vividsolutions.jts.io.WKTReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* @author yx
* @date 2018年6月5日
* @desc
*/
public class GdalUtil {
/**
* @param args
*/
public static void main(String[] args) {
// String wkt="POLYGON((114.602459391557 37.613819833822, 114.603906851903 37.613747408903, 114.603688890869 37.612570240952, 114.602161141124 37.61279560696, 114.602459391557 37.613819833822))";
// String s = geoToJson(wkt);
// System.out.println(s);
String json="{\"type\":\"Polygon\",\"coordinates\":[[[114.602459391557,37.613819833822],[114.603906851903,37.613747408903],[114.603688890869,37.612570240952],[114.602161141124,37.61279560696],[114.602459391557,37.613819833822]]]}";
String s = jsonToWkt(json);
System.out.println(s);
}
/**
* wkt转json
*
* @param wkt
* @return
*/
public static String geoToJson(String wkt) {
String json = null;
try {
WKTReader reader = new WKTReader();
com.vividsolutions.jts.geom.Geometry geometry = reader.read(wkt);
StringWriter writer = new StringWriter();
// 保留坐标的小数
GeometryJSON g = new GeometryJSON(12);
g.write(geometry, writer);
json = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
/**
* json转wkt
*
* @param geoJson
* @return
*/
public static String jsonToWkt(String geoJson) {
String wkt = null;
GeometryJSON gjson = new GeometryJSON();
Reader reader = new StringReader(geoJson);
try {
com.vividsolutions.jts.geom.Geometry geometry = gjson.read(reader);
wkt = geometry.toText();
} catch (IOException e) {
e.printStackTrace();
}
return wkt;
}
/**
* 解析shp数据并入库
*/
@SuppressWarnings("unused")
public JSONArray decodeShapeFile(String path) {
gdal.AllRegister();
// 为了支持中文路径,请添加下面这句代码
gdal.SetConfigOption("G