GeoTools读取Shape File


package com.test.geotools.shp;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.Geometry;


public class shptest {

public static void main(String[] argv){

String strShpPath = "D:\\data\\country.shp";
File file = new File(strShpPath);
ShapefileDataStore shpDataStore = null;
try{
shpDataStore = new ShapefileDataStore(file.toURL());
String typeName = shpDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();

FeatureIterator<SimpleFeature> itertor = result.features();
while(itertor.hasNext()){
SimpleFeature feature = itertor.next();
Geometry geometry = (Geometry)feature.getDefaultGeometry();
System.out.println(geometry.toText());
}
itertor.close();
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}

转载自:https://blog.csdn.net/marsrprj/article/details/83567057

You may also like...