geotools读取shapefile文件

依赖

  1. <dependency>  
  2.             <groupId>org.geotools</groupId>  
  3.             <artifactId>gt-shapefile</artifactId>  
  4.             <version>${geotools.version}</version>  
  5.         </dependency>  

 

代码

  1. File file = new File(“D:\\shapefiles\\states.shp”);  
  2.        Map<String, Object> map = new HashMap<String, Object>();  
  3.        map.put(“url”, file.toURI().toURL());  
  4.   
  5.        DataStore dataStore = DataStoreFinder.getDataStore(map);  
  6.        String typeName = dataStore.getTypeNames()[0];  
  7.   
  8.        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore  
  9.                .getFeatureSource(typeName);  
  10.   
  11.        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();  
  12.        FeatureIterator<SimpleFeature> features = collection.features();  
  13.            while (features.hasNext()) {  
  14.                SimpleFeature feature = features.next();  
  15.                System.out.print(feature.getID());  
  16.                System.out.print(“: “);  
  17.                System.out.println(feature.getDefaultGeometryProperty().getValue());//此行输出的空间信息的wkt表达形式  
  18.            }

转载自:https://blog.csdn.net/xiaohan2826/article/details/53860387

You may also like...