IDEA Java解析GeoJson.json文件
程序员文章站
2022-03-26 21:59:53
一、遇到的问题1. 无法导入成功2. org.geotools.StyleFactory is not an ImageIO SPI class解决: https://ask.csdn.net/questions/4105133调整为jdk1.8就ok了3. Error parsing epsg:4326 as crs id换一种geojson的crs 的json串4. resolution will not be reattempted until the update interval o...
一、遇到的问题
1. 无法导入成功
2. org.geotools.StyleFactory is not an ImageIO SPI class
解决: https://ask.csdn.net/questions/4105133
调整为jdk1.8就ok了
3. Error parsing epsg:4326 as crs id
换一个geojson示例的crs json串
4. resolution will not be reattempted until the update interval of XXX has elapsed or updates are force
解决: https://blog.csdn.net/u013066244/article/details/91986308
5. Exception java.lang.NoClassDefFoundExcepthon: org.geotools.util.factory.FactoryFinder
解决:所有的maven pom依赖都不能少
二、源码
1. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jjxliu.geotools</groupId>
<artifactId>geojson-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>geojson-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>17.1</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>22.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- for geotools begin -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>24.2</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>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-jdbc</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<!--for geotools end-->
<dependency>
<groupId>org.opengeo</groupId>
<artifactId>geodb</artifactId>
<version>0.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.bedatadriven</groupId>
<artifactId>jackson-datatype-jts</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.locationtech.jts/jts-core -->
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.gdal/gdal -->
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>3.2.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<!-- <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
<!--不加如下updatePolicy会报错:resolution will not be reattempted until the update interval of XXX has elapsed or updates are force-->
<updatePolicy>always</updatePolicy>
</releases>
</repository>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.4.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. 源码
package com.test;
import com.test.geojson.FeatureJSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.geotools.data.DataUtilities;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/*************************************
*Class Name: GeoJsonUtil
*Description: <GeoJsonUtil工具类,重写FeatureJSON>
*@create: 2021/2/23
*@since 1.0.0
*************************************/
@Slf4j
public class GeoJsonUtil {
public void lineString2FeatureCollection() throws IOException, SchemaException, ParseException {
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 = new GeometryFactory();
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());
}
public void point2FeatureCollection() throws IOException, SchemaException, ParseException {
String[] WKTS = {"POINT (255351.04293761664 474966.9279243938 1.0)",
"POINT (255529.29662365236 474272.4599921228 2.0)",
"POINT (256166.05830998957 473979.44920198264 3.111)"};
// 坐标顺序是EAST_NORTH,即经度在前
String json = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"area\":3865207830, \"text\": null},\"id\":\"polygon.1\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[116.19827270507814,39.78321267821705],[116.04446411132814,39.232253141714914],[116.89590454101562,39.3831409542565],[116.86981201171876,39.918162846609455],[116.19827270507814,39.78321267821705]]]}}],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}";
FeatureJSON fjson_15 = new FeatureJSON(new GeometryJSON(15));
CoordinateReferenceSystem crs = fjson_15.readCRS(json);
final SimpleFeatureType TYPE = DataUtilities.createType("Point",
"geometry:Point," + // <- the geometry attribute: Point type
"id:String," + // <- a String attribute
"mag:Integer" // <- 权重,叠加次数
);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
GeometryFactory geometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(geometryFactory);
FeatureJSON fjson = new FeatureJSON();
List<SimpleFeature> features = new ArrayList<>();
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
for (String wkt : WKTS) {
Point point = (Point) reader.read(wkt);
featureBuilder.add(point);
featureBuilder.add("1");
featureBuilder.add(2);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
StringWriter writer = new StringWriter();
fjson.setEncodeFeatureCollectionCRS(true);
fjson.writeFeatureCollection(collection, writer);
System.out.println(writer.toString());
}
public static void main(String[] args) throws ParseException, IOException, SchemaException {
GeoJsonUtil geoJsonUtil = new GeoJsonUtil();
geoJsonUtil.point2FeatureCollection();
List<String> list1 = new ArrayList<>();
list1.add("1");
list1.add("2");
list1.add("3");
String deArr[] = list1.toArray(new String[list1.size()]);
String str = "('" + StringUtils.join(deArr, "','") + "')";
log.info("str: {}", str);
}
}
3. geojson示例文件
{
"type": "FeatureCollection",
"features": [
{
"properties": {
"version": 0
},
"id": "1",
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
1868732.467515988,
6594375.3042187244
],
[
1868732.467515988,
5165920.1196253505
],
[
3297187.6521093622,
5165920.1196253505
],
[
3297187.6521093622,
6594375.3042187244
],
[
1868732.467515988,
6594375.3042187244
]
]
]
}
}
]
}
三、注意
- jdk1.8
- 配置repository updatePolicy
- org.locationtech.jts.geom.GeometryFactory; 与 com.vividsolutions.jts.geom.GeometryFactory; 用哪个包就导入哪个的所有类
- gdal竟然导入成功了,原来之前导不进来是因为没有配置repository;
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
<!--不加如下updatePolicy会报错:resolution will not be reattempted until the update interval of XXX has elapsed or updates are force-->
<updatePolicy>always</updatePolicy>
</releases>
</repository>
</repositories>
参考
- Maven中GeoTools的引入 - Maven 的 repository 与 mirror
- geojson格式介绍
- https://www.codeleading.com/article/62773085591/
- GeoJson的生成与解析
- Create shapefile based on geojson data with geotools (Java)
本文地址:https://blog.csdn.net/qq_40985985/article/details/113979329
上一篇: Java安装及配置JDK/JRE