0
点赞
收藏
分享

微信扫一扫

java使用geotools解析矢量数据kml、geojson、shp文件

田妞的读书笔记 2023-11-12 阅读 111
javageotools

geotools解析kml、geojson

geotools环境准备

这里使用的是maven引用geotools包,引用geotools包需要添加maven仓库,pom.xml文件如下:

<properties>
	<!-- geotools版本 -->
	<geotools-version>28.2</geotools-version>
</properties>

<!-- geotools-->
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-main</artifactId>
	<version>${geotools-version}</version>
</dependency>
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-geojson</artifactId>
	<version>${geotools-version}</version>
</dependency>
	<!-- geotools-geojson核心包 -->
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-geojson-core</artifactId>
	<version>${geotools-version}</version>
</dependency>
<dependency>
	<groupId>org.geotools.xsd</groupId>
	<artifactId>gt-xsd-kml</artifactId>
	<version>${geotools-version}</version>
</dependency>

<!-- geotools仓库-->
<repositories>
	<repository>
		<id>osgeo</id>
		<name>Open Source Geospatial Foundation Repository</name>
		<url>https://repo.osgeo.org/repository/release/</url>
	</repository>
	<repository>
		<id>osgeo-snapshot</id>
		<name>OSGeo Snapshot Repository</name>
		<url>https://repo.osgeo.org/repository/snapshot/</url>
	</repository>
</repositories>

公共获取属性方法

@Slf4j
public class AnalysisUtil {
	public static void readProperty(SimpleFeature simpleFeature) {
        Collection<Property> properties = simpleFeature.getProperties();
        Iterator<Property> iterator = properties.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            Property property = iterator.next();
            //kml拿到属性值会有自带的9个属性
            if (index++ > 8) {
                log.info("GeoJSONReader解析geojson -->> 属性名:【{}】,属性值:【{}】,属性类型:【{}】", property.getName().toString(),
                        property.getValue(), property.getType().getBinding());
            }
        }
        Object defaultGeometry = simpleFeature.getDefaultGeometry();
        //wkt格式geometry
        Geometry geometry = (Geometry) defaultGeometry;
        log.info("wkt格式geometry:{}", geometry);
    }
}

解析kml

public class AnalysisKml {
	public static void main(String[] args) {
        String fileUrl = "D:\\workspace\\vector\\vector\\KML.kml";
        try (FileInputStream fileInputStream = new FileInputStream(fileUrl)) {
            PullParser parser = new PullParser(new KMLConfiguration(), fileInputStream, SimpleFeature.class);
            SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
            //kml文件声明了坐标系才能获取到,没声明获取就是null
            CoordinateReferenceSystem coordinateReferenceSystem = simpleFeature.getFeatureType().getCoordinateReferenceSystem();
            log.info("解析kml获取坐标系:{}", coordinateReferenceSystem);
            readKml(simpleFeature, parser);
        } catch (XMLStreamException | IOException | SAXException e) {
            throw new UtilException(e.getMessage());
        }
    }
    
	//递归方式获取每个地块信息
    public static void readKml(SimpleFeature simpleFeature, PullParser parser) throws XMLStreamException, IOException,
            SAXException {
        //读取属性
        AnalysisUtil.readProperty(simpleFeature);
        //获取下一个simpleFeature
        while (simpleFeature != null && simpleFeature.getDefaultGeometry() != null) {
            simpleFeature = (SimpleFeature) parser.parse();
            readKml(simpleFeature, parser);
        }
    }
}

解析geojson

@Slf4j
public class GeoJsonAnalysis {
	public static void main(String[] args) {
        String fileUrl = "D:\\workspace\\vector\\vector\\福田路网geojson.geojson";
		geoJSONReader(fileUrl);
        featureJson(fileUrl);
    }

    public static void featureJson(String fileUrl) {
        // 指定GeometryJSON构造器,15位小数
        FeatureJSON featureJson = new FeatureJSON(new GeometryJSON(15));
        try {
            FeatureCollection featureCollection = featureJson.readFeatureCollection(new FileInputStream(fileUrl));
            //获取坐标系
            CoordinateReferenceSystem coordinateReferenceSystem =
                    featureCollection.getSchema().getCoordinateReferenceSystem();
            log.info("解析geojson获取坐标系:{}", coordinateReferenceSystem);
            FeatureIterator featureIterator = featureCollection.features();
            while (featureIterator.hasNext()) {
                SimpleFeature simpleFeature = (SimpleFeature) featureIterator.next();
                AnalysisUtil.readProperty(simpleFeature);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void geoJSONReader(String fileUrl) {
        GeoJSONReader reader = null;
        try {
            reader = new GeoJSONReader(new FileInputStream(fileUrl));
            SimpleFeatureCollection featureCollection = reader.getFeatures();
            //创建图层数据迭代器
            FeatureIterator<SimpleFeature> simpleFeatureFeatureIterator = featureCollection.features();
            while (simpleFeatureFeatureIterator.hasNext()) {
                AnalysisUtil.readProperty(simpleFeatureFeatureIterator.next());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

解析shp

shp文件解析请看往期文章:geotoolsu解析shp文件

举报

相关推荐

0 条评论