geotools 转 geojson字符串
pom
<!-- https://mvnrepository.com/artifact/org.geotools.jdbc/gt-jdbc-postgis --> <dependency> <groupId>org.geotools.jdbc</groupId> <artifactId>gt-jdbc-postgis</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>24.0</version> </dependency> <!-- Provides support for PostGIS. Note the different groupId --> <dependency> <groupId>org.geotools.jdbc</groupId> <artifactId>gt-jdbc-postgis</artifactId> <version>24.0</version> </dependency> <!-- Provides support for shapefiles --> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>24.0</version> </dependency> <!-- Provides GUI components --> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swing</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-referencing</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-api</artifactId> <version>21-SNAPSHOT</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-opengis</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-referencing</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-render</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools.xsd</groupId> <artifactId>gt-xsd-sld</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-data</artifactId> <version>21-SNAPSHOT</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-xml</artifactId> <version>24.0</version> </dependency> <repositories> <repository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> </repository> <!-- include central so that it is searched before our alternate repos --> <repository> <id>central</id> <name>Maven Repository Switchboard</name> <layout>default</layout> <url>https://repo1.maven.org/maven2</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>locationtech-releases</id> <url>https://repo.locationtech.org/content/groups/releases</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <!-- geotools --> <repository> <id>boundlessgeo</id> <url>https://repo.boundlessgeo.com/main</url> </repository> <!-- jai --> <repository> <id>osgeo</id> <url>https://repo.osgeo.org/repository/release/</url> </repository> <repository> <id>geomajas</id> <url>http://maven.geomajas.org/</url> </repository> </repositories>
合并面
try { Geometry resultGeometry = null; GeometryJSON geometryJson = new GeometryJSON(); for (SDataVo childData : childDatas){ String one = "{"type":"Polygon","coordinates":[[[104.0555,30.6226],[104.055,30.622],[104.0551,30.6114],[104.0545,30.6104],[104.0551,30.6096],[104.0581,30.6093],[104.0579,30.6006],[104.0574,30.5776],[104.0542,30.5641],[104.0543,30.5448],[104.0345,30.5447],[104.0345,30.5537],[104.0378,30.5537],[104.0391,30.5638],[104.0314,30.5656],[104.0349,30.5694],[104.0351,30.5725],[104.0329,30.5724],[104.0339,30.5747],[104.0319,30.5753],[104.0319,30.5777],[104.0311,30.5782],[104.0316,30.5798],[104.0307,30.581],[104.0284,30.5818],[104.0277,30.58],[104.0267,30.5809],[104.0257,30.58],[104.0248,30.5805],[104.0249,30.5816],[104.0226,30.5808],[104.0204,30.5801],[104.0173,30.5821],[104.0168,30.5846],[104.0185,30.587],[104.0196,30.5874],[104.0191,30.5878],[104.0211,30.5877],[104.02,30.5893],[104.0226,30.5935],[104.0261,30.5927],[104.0273,30.5932],[104.028,30.5957],[104.0277,30.5967],[104.0295,30.5981],[104.0291,30.5999],[104.0301,30.6033],[104.0295,30.6041],[104.0238,30.6045],[104.0232,30.608],[104.0238,30.608],[104.0237,30.6114],[104.0399,30.6023],[104.0439,30.6055],[104.0409,30.613],[104.036,30.6154],[104.05,30.6372],[104.0514,30.6361],[104.056,30.6357],[104.0555,30.6226]]]}"; Geometry read = geometryJson.read(new StringReader(one)); if (ObjectUtil.isNull(read)){ continue; } if (ObjectUtil.isNull(resultGeometry)){ resultGeometry = read; continue; } OverlayOp op = new OverlayOp(resultGeometry,read); resultGeometry = op.getResultGeometry(OverlayOp.UNION); } StringWriter writer = new StringWriter(); geometryJson.write(resultGeometry,writer);
System.out.println(writer.toString());
} catch (IOException e) { e.printStackTrace(); }