pg数据库添加postgis扩展以及生成空间数据

 pg数据库扩展:create extension postgis
 执行后,刷新表格,会生成如图所示表:
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019031410591918.png)
 项目,我使用的是spingboot,
 在application.yml文件中添加:
    jpa:
	    show-sql: true    //是否显示sql语句
	    hibernate:
	      ddl-auto: update    /初始化数据库新增表
	    properties:
	        hibernate:
	            dialect: org.hibernate.spatial.dialect.postgis.PostgisPG9Dialect    //数据库方言

   entity文件中:
   	@JsonSerialize(using = GeometrySerializer.class)    //序列化方法
    @JsonDeserialize(using = GeometryDerializer.class)   //反序列化方法
    private Geometry coordinate;   //存储空间地理字段字段

	public static class GeometrySerializer extends JsonSerializer<Geometry>{
		@Override
		public void serialize(Geometry geometry, JsonGenerator gen, SerializerProvider serializers)
				throws IOException{
				String wkt =  null;
				if(geometry != null){
					wkt = geometry.toText();
					gen.writeString(wkt);
				}
		 }
	 }
	public static class GeometryDerializer extends JsonDeserializer<Geometry> {
		@Override
		public Geometry deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
			String wkt = p.getText();
			Geometry geometry = null;
				try {
					if(wkt.startsWith("SRID")){
						String[] strarray = wkt.split(";");
						int srid = Integer.parseInt(strarray[0].substring(5));
						wkt = strarray[1];
						geometry  = new WKTReader().read(wkt);
						geometry.setSRID(srid);
					}else{
						geometry  = new WKTReader().read(wkt);
						geometry.setSRID(4326);
					}
				}catch (Exception e) {
					throw new JsonParseException("Geometry反序列化失败");
				}
				return geometry;
			}
		}

     在impl方法中:

		if(job.has("coordinate")){ //查看JSONObject是否包含coordinate字段
			JSONArray coordinate = job.getJSONArray("coordinate");
			Geometry geometry = this.getBboxGeometry(coordinate);
		}

public Geometry getBboxGeometry(JSONArray coordinate) {
	Geometry dataBoundary = null;
    try {
	        Double minx = coordinate.getDouble(0);
	        Double miny = coordinate.getDouble(1);
	        Double maxx = coordinate.getDouble(2);
	        Double maxy = coordinate.getDouble(3);
	        int srid = Integer.valueOf("4326");
	        // String wktString = String.format("POLYGON((%s %s,%s %s,%s %s,%s %s,%s %s))", minX, minY, maxX, minY, maxX, maxY, minX, maxY, minX, minY);
	        //四个点成一个矩形面
		    String polygon = "POLYGON((" + minx + " " + miny + ", " + minx + " " + maxy + ", " + maxx + " " + maxy + ", " + maxx + " " + miny + ", " + minx + " " + miny + "))";
            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
            WKTReader reader = new WKTReader(geometryFactory);
            dataBoundary = reader.read(polygon);
            dataBoundary.setSRID(srid);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return dataBoundary;
    }


  pom文件中可能用到的包:
  	<dependency>
	    <groupId>org.hibernate</groupId>
	    <artifactId>hibernate-spatial</artifactId>
	    <version>5.2.10.Final</version>
	</dependency>
	<dependency>
		<groupId>org.postgresql</groupId>
		<artifactId>postgresql</artifactId>
		<scope>runtime</scope>
	</dependency>
    <dependency>
		<groupId>org.postgis</groupId>
		<artifactId>postgis-jdbc</artifactId>
		<version>1.3.3</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-geotiff</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-geojson</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-geometry</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-image</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-render</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-imagemosaic</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-epsg-hsql</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-process-raster</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-imageio-ext-gdal</artifactId>
		<version>${geotools.version}</version>
	</dependency>
	<dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-shapefile</artifactId>
		<version>${geotools.version}</version>
	</dependency>
    <dependency>
		<groupId>org.geotools</groupId>
		<artifactId>gt-metadata</artifactId>
		<version>19.0</version>
	</dependency>
	<dependency>
		<groupId>org.geotools.xsd</groupId>
		<artifactId>gt-xsd-sld</artifactId>
		<version>19.0</version>
	</dependency>
	<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
	</dependency>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<geotools.version>19.2</geotools.version>
		<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
	</properties>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值