依赖
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</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></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
验证是否存在无效数据(如边界超出范围)
// Rather than use an iterator, create a FeatureVisitor to check each fature
static class ValidationVisitor implements FeatureVisitor {
public int numInvalidGeometries = 0;
@Override
public void visit(Feature f) {
SimpleFeature feature = (SimpleFeature) f;
Geometry geom = (Geometry) feature.getDefaultGeometry();
// 这里是核心,直接调用方法判断geom是否有效
if (geom != null && !geom.isValid()) {
numInvalidGeometries++;
System.out.println("Invalid Geoemtry: " + feature.getID());
}
}
}
public static void main(String[] args) throws IOException {
// 读取数据
File file = new File("E:\\DataofGeotools\\data.shp");
FileDataStore store = FileDataStoreFinder.getDataStore(file);
FeatureSource featureSource = store.getFeatureSource();
SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featureSource.getFeatures();
ValidationVisitor visitor = new ValidationVisitor();
// 验证
// Visit the contents of a feature collection.
simpleFeatureCollection.accepts(visitor,null);
System.out.println(visitor.numInvalidGeometries);
}
坐标转换
1、过程:
读取data.shp,进行投影转换之后写出
public class Reproject {
public static void main(String[] args) throws IOException, FactoryException {
File file = new File("E:\\DataofGeotools\\data.shp");
FileDataStore store = FileDataStoreFinder.getDataStore(file);
FeatureSource featureSource = store.getFeatureSource();
SimpleFeatureType type = (SimpleFeatureType) featureSource.getSchema();
// 源坐标
CoordinateReferenceSystem sourceCRS = type.getCoordinateReferenceSystem();
// 目标坐标
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4214");
// allow for some error due to different datums
boolean lenient = true;
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, lenient);
// 重新写文件
// 获取到要素集合
SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) featureSource.getFeatures();
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<>();
File newShpFile = new File("E:\\DataofGeotools\\dataproject11.shp");
create.put("url", newShpFile.toURI().toURL());
create.put("create spatial index", Boolean.TRUE);
DataStore dataStore = dataStoreFactory.createNewDataStore(create);
SimpleFeatureType featureType = SimpleFeatureTypeBuilder.retype(type, targetCRS);
dataStore.createSchema(featureType);
// Get the name of the new Shapefile, which will be used to open the FeatureWriter
String createdName = dataStore.getTypeNames()[0];
Transaction transaction = new DefaultTransaction("Reproject");
try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriterAppend(createdName, transaction);
SimpleFeatureIterator iterator = featureCollection.features()) {
while (iterator.hasNext()) {
// copy the contents of each feature and transform the geometry
SimpleFeature feature = iterator.next();
SimpleFeature copy = writer.next();
copy.setAttributes(feature.getAttributes());
Geometry geometry = (Geometry) feature.getDefaultGeometry();
Geometry geometry2 = JTS.transform(geometry, transform);
copy.setDefaultGeometry(geometry2);
writer.write();
}
// 事务提交以前一定要记得关闭资源
writer.close();
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
}
// 可以筛选你想要的要素进行投影转换输出
Query query = new Query(typeName);
query.setCoordinateSystemReproject(map.getCoordinateReferenceSystem());
SimpleFeatureCollection featureCollection = featureSource.getFeatures(query);
2、常用坐标:
EPSG:4326
EPSG Projection 4326 - WGS 84
EPSG: 3785
Popular Visualization CRS / Mercator
EPSG:3005
NAD83 / BC Albers
坐标查询地址:https://epsg.io/
常见坐标获取及创建
1、通过simplefetatureType获取
SimpleFeatureType type = (SimpleFeatureType) featureSource.getSchema();
CoordinateReferenceSystem sourceCRS = type.getCoordinateReferenceSystem();
2、通过CRS
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4214");
3、通过CRS Factory
CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");
4、DefaultGeographicCRS
CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;