Geotools中Geometry对象与GeoJson的相互转换
来源:互联网 发布:ubuntu查看磁盘分区 编辑:程序博客网 时间:2024/06/10 21:39
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是不支持的,只支持floatfinal 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对象的情况,等遇到再研究吧。
- Geotools中Geometry对象与GeoJson的相互转换
- geotools中shp和geojson格式的相互转换
- GeoJson格式与转换(shapefile)Geotools
- Spring Boot(MVC)下空间字段(Geometry)与geojson的自动转换
- ArcMap中使用ArcPy实现Geometry与WKT的相互转换
- ArcMap中使用ArcPy实现Geometry与WKT的相互转换
- android arcgis入门(八)、Json与Geometry的相互转换
- C# 中 json对象与字符串的相互转换
- JS 中 JSON 与对象 的相互转换
- Java中Json字符串与对象之间的相互转换
- springmvc中对象与字符串的相互转换
- GeoTools应用-JTS(Geometry之间的关系)
- GeoTools应用-JTS(Geometry之间的关系)
- GeoTools应用-JTS(Geometry之间的关系)
- JSON与java对象的相互转换
- xml与java对象的相互转换
- json串与对象的相互转换
- Java对象与Json的相互转换
- HPU-1194-Judge [快速幂]
- Python自动化运维学习(一)
- python中线程进程
- lintcode:爬楼梯(递归,迭代,动态规划)
- Trie树(字典树)
- Geotools中Geometry对象与GeoJson的相互转换
- 2440: [中山市选2011]完全平方数
- stm32关于莫名进硬件中断,串口发数卡死等原因的其中之一
- 51nod 1012 最小公倍数LCM
- MapReduce 的格式输入----SequenceFileInputFormat ---源码分析
- 简单规律 HDU
- 短时平均幅度函数
- 24.odoo入门——工作杂记
- 查一波查找算法