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


	
		UTF-8
		UTF-8
		1.8
		19.2
		Greenwich.RC2
	

你可能感兴趣的:(数据库相关)