记一次geometry入库操作及shp下载

本文记录了一次后端开发经验,涉及将前端传来的GeoJSON数据存储到MySQL数据库,以及如何从数据库中导出Shapefile供下载。主要思路包括将GeoJSON转化为WKT格式入库,使用特定SQL函数进行数据操作,并通过工具类生成SHP文件。
摘要由CSDN通过智能技术生成

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数据,入库的操作步骤如下

  1. 转为可入库的数据类型 ----将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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灵湖映北辰

年轻人,要讲武德!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值