GeoTools获取shp文件地理坐标系、投影、字段信息和坐标geometry数据

创建maven工程,加载gt-shapefile依赖包。注意,仓库应该放在依赖后面才能把包都加载完。

1. pom.xml文件



    4.0.0

    org.example
    untitled
    1.0-SNAPSHOT

    
        UTF-8
        23-SNAPSHOT
    

    
        
            org.geotools
            gt-shapefile
            ${geotools.version}
        
    

    
        
            maven2-repository.dev.java.net
            Java.net repository
            http://download.java.net/maven/2
        
        
            osgeo
            Open Source Geospatial Foundation Repository
            http://download.osgeo.org/webdav/geotools/
        
        
            
                true
            
            boundless
            Boundless Maven Repository
            http://repo.boundlessgeo.com/main
        
    

2. 创建获取shp要素信息Java文件

package cn.com.geotool;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.GeometryType;
import org.opengis.feature.type.Name;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName GetShpMsg
 * @Author
 * @Date 2020/4/23
 * @Description TODO
 * @Version 1.0
 */
public class GetShpMsg {

    public static void main(String[] args) throws IOException {
        String pathName = "D:\\GeoToolsTestData\\locations.shp";
        File file = new File(pathName);

        // 读取到数据存储中
        FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
        // 获取特征资源
        SimpleFeatureSource simpleFeatureSource = dataStore.getFeatureSource();
        // 要素集合
        SimpleFeatureCollection  simpleFeatureCollection = simpleFeatureSource.getFeatures();

        // 要素数量
        int featureSize = simpleFeatureCollection.size();
        // 获取要素迭代器
        SimpleFeatureIterator featureIterator = simpleFeatureCollection.features();
        if(featureIterator.hasNext()){
            // 要素对象
            SimpleFeature feature = featureIterator.next();
            // 要素属性信息,名称,值,类型
            List propertyList = (List) feature.getValue();
            for(Property property : propertyList){
              System.out.println("属性名称:" + property.getName());
              System.out.println("属性值:" + property.getValue());
              System.out.println("属性类型:" + property.getType());
              System.out.println();
            }

            // 要素属性信息
            List featureAttributes = feature.getAttributes();

            // 要素geometry的类型和坐标,如点,线,面及其组成的坐标
            Object geometryText = feature.getDefaultGeometry();

            // geometry属性
            GeometryAttribute geometryAttribute = feature.getDefaultGeometryProperty();
            // 获取坐标参考系信息
            CoordinateReferenceSystem coordinateReferenceSystem = geometryAttribute.getDescriptor().getCoordinateReferenceSystem();

            // geometry类型
            GeometryType geometryType = geometryAttribute.getType();
            // geometry类型名称
            Name name = geometryType.getName();

            System.out.println("要素数量:"+ featureSize);
            System.out.println("要素属性:" + featureAttributes);
            System.out.println("要素geometry位置信息:" + geometryText);
            System.out.println("要素geometry类型名称:" + name);
            System.out.println("shp文件使用的坐标参考系:\n" + coordinateReferenceSystem);

        }

    }
}
 
  

3.输出结果

属性名称:the_geom
属性值:POINT (11.116667 46.066667)
属性类型:GeometryTypeImpl Point

属性名称:Name
属性值:Trento
属性类型:AttributeTypeImpl Name
restrictions=[ length([.]) <= 15 ]

属性名称:number
属性值:140
属性类型:AttributeTypeImpl number
restrictions=[ length([.]) <= 9 ]

要素数量:15
要素属性:[POINT (11.116667 46.066667), Trento, 140]
要素geometry位置信息:POINT (11.116667 46.066667)
要素geometry类型名称:Point
shp文件使用的坐标参考系:
GEOGCS["WGS84(DD)", 
  DATUM["WGS84", 
    SPHEROID["WGS84", 6378137.0, 298.257223563]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic longitude", EAST], 
  AXIS["Geodetic latitude", NORTH]]

4. locations.shp文件截图

GeoTools获取shp文件地理坐标系、投影、字段信息和坐标geometry数据_第1张图片

你可能感兴趣的:(GeoTools,gis)