当前位置:   article > 正文

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

pg数据库postgs扩展
 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/589812
推荐阅读
相关标签
  

闽ICP备14008679号