Geotools中Geometry对象与GeoJson的相互转换
目录
maven依赖
<geotools.version>17.1</geotools.version>
<!-- for geotools begin -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-main -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>${geotools.version}</version>
</dependency>
<!--for geotools end-->
引用情况
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.linemerge.LineMerger;
import org.geotools.data.DataUtilities;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.*;
Geometry的子类主要有Point, LineString和Polygon. 基本操作类似。所以此处以LineString为例说明
LineString–>geojson
// 由wkt字符串构造LineString对象
WKTReader reader = new WKTReader( geometryFactory );
LineString lineString = (LineString)reader.read("LINESTRING (254058.76074485347 475001.2186020431, 255351.04293761664 474966.9279243938)");
// 设置保留6位小数,否则GeometryJSON默认保留4位小数
GeometryJSON geometryJson = new GeometryJSON(6);
StringWriter writer = new StringWriter();
geometryJson.write(lineString, writer);
System.out.println(writer.toString());
writer.close();
geojson–>LineString
LineString lineString = (LineString) geometryJson.read(new StringReader("{\n" +
" \"type\": \"LineString\",\n" +
" \"coordinates\": [\n" +
" [\n" +
" 120.6584555,\n" +
" 30.45144\n" +
" ],\n" +
" [\n" +
" 120.1654515,\n" +
" 30.54848\n" +
" ]\n" +
" ]\n" +
" }"));
geojson还定义了带属性的Feature和表示Geometry对象集合的FeatureCollection,构造方法更加复杂,写两个示例备忘吧。
LineString转Feature
// geometry是必须的,其他属性可根据需求自定义,但是支持的类型有限,例如这个版本中double是不支持的,只支持float
final SimpleFeatureType TYPE = DataUtilities.createType("Link",
"geometry:LineString," + // <- the geometry attribute: Point type
"gid:String," + // <- a String attribute
"direction:Integer," + // a number attribute
"orientation:Integer"
);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
WKTReader reader = new WKTReader( geometryFactory );
FeatureJSON fjson = new FeatureJSON();
LineString lineString = (LineString)reader.read("LINESTRING (254058.76074485347 475001.2186020431, 255351.04293761664 474966.9279243938)");
// 按照TYPE中声明的顺序为属性赋值就可以,其他方法我暂未尝试
featureBuilder.add(lineString);
featureBuilder.add("123456");
featureBuilder.add(2);
featureBuilder.add(0);
SimpleFeature feature = featureBuilder.buildFeature(null);
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
System.out.println(writer.toString());
执行结果如下:
{“type”:”Feature”,”geometry”:{“type”:”LineString”,”coordinates”:[[254058.7607,475001.2186],[255351.0429,474966.9279]]},”properties”:{“gid”:”123456”,”direction”:2,”orientation”:0},”id”:”fid–5b8f258_15e04bda4b8_-8000”}
Feature转LineString
应该与GeometryJson中的方法类似,因暂未遇到此需求,就先空着吧。随缘。。。。
LineString转FeatureCollection
String[] WKTS = { "LINESTRING (255351.04293761664 474966.9279243938, 255529.29662365236 474272.4599921228)",
"LINESTRING (255529.29662365236 474272.4599921228, 256166.05830998957 473979.44920198264)"};
final SimpleFeatureType TYPE = DataUtilities.createType("Link",
"geometry:LineString," + // <- the geometry attribute: Point type
"gid:String," + // <- a String attribute
"direction:Integer," + // a number attribute
"orientation:Integer"
);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
WKTReader reader = new WKTReader( geometryFactory );
FeatureJSON fjson = new FeatureJSON();
List<SimpleFeature> features = new ArrayList<>();
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
for (String wkt : WKTS) {
LineString lineString = (LineString)reader.read(wkt);
featureBuilder.add(lineString);
featureBuilder.add("123456");
featureBuilder.add(2);
featureBuilder.add(1);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
StringWriter writer = new StringWriter();
fjson.writeFeatureCollection(collection, writer);
System.out.println(writer.toString());
打印结果如下:
{“type”:”FeatureCollection”,”features”:[{“type”:”Feature”,”geometry”:{“type”:”LineString”,”coordinates”:[[255351.0429,474966.9279],[255529.2966,474272.46]]},”properties”:{“gid”:”123456”,”direction”:2,”orientation”:1},”id”:”fid-67c46b85_15e0778dd81_-8000”},{“type”:”Feature”,”geometry”:{“type”:”LineString”,”coordinates”:[[255529.2966,474272.46],[256166.0583,473979.4492]]},”properties”:{“gid”:”123456”,”direction”:2,”orientation”:1},”id”:”fid-67c46b85_15e0778dd81_-7fff”}]}
对于FeatureCollection中包含不同类型Geometry对象的情况,等遇到再研究吧。
转载自:https://blog.csdn.net/lnxyangruosong/article/details/77456735