基于GeoTools,给定一条多段线,计算并获取从起点开始500米处的点坐标
时间: 2024-03-21 08:41:41 浏览: 196
可以使用以下代码基于GeoTools,给定一条多段线,计算并获取从起点开始500米处的点坐标:
```java
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.JTSUtilities;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.*;
import org.locationtech.jts.geom.*;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.identity.FeatureId;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GeoToolsLine500m {
public static void main(String[] args) throws Exception {
// 加载多段线
File file = new File("line.shp");
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createDataStore(file.toURI().toURL());
dataStore.setCharset(org.geotools.data.shapefile.ShpFiles.UTF_8);
String typeName = dataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator featureIterator = featureCollection.features();
// 获取FeatureCollection的坐标系
CoordinateReferenceSystem crs = featureCollection.getSchema().getCoordinateReferenceSystem();
// 创建GeometryFactory
GeometryFactory geomFactory = JTSFactoryFinder.getGeometryFactory();
// 创建GeodeticCalculator
GeodeticCalculator calculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
// 遍历每个Feature
while (featureIterator.hasNext()) {
SimpleFeature feature = featureIterator.next();
Geometry geometry = (Geometry) feature.getDefaultGeometry();
// 遍历每个MultiLineString
if (geometry instanceof MultiLineString) {
MultiLineString multiLineString = (MultiLineString) geometry;
for (int i = 0; i < multiLineString.getNumGeometries(); i++) {
LineString lineString = (LineString) multiLineString.getGeometryN(i);
// 计算每个LineString的起点
Coordinate startCoord = lineString.getCoordinateN(0);
Point startPoint = geomFactory.createPoint(startCoord);
// 设置计算起点
calculator.setStartingGeographicPoint(startCoord.x, startCoord.y);
// 计算距离起点500米的点坐标
calculator.setDirection(0, 500);
Point point500m = null;
try {
double[] dest = calculator.getDestinationPosition();
point500m = geomFactory.createPoint(new Coordinate(dest[0], dest[1]));
} catch (Exception e) {
e.printStackTrace();
}
// 将点坐标转换到FeatureCollection的坐标系
if (point500m != null) {
try {
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crs);
point500m = (Point) JTS.transform(point500m, transform);
} catch (Exception e) {
e.printStackTrace();
}
}
// 输出点坐标
if (point500m != null) {
System.out.println("Lon: " + point500m.getX() + ", Lat: " + point500m.getY());
}
}
}
}
featureIterator.close();
dataStore.dispose();
}
}
```
需要注意的是,该代码使用的是WGS84坐标系,如果需要使用其他坐标系,需要修改代码中的相关部分。
阅读全文