我有一个读取一组坐标的函数,并且获得了UTM坐标。稍后,我将这些UTM坐标转换为WGS84。当这些数据来自4个文件集(shp,.dbf,.shx和.prj)时,没有问题。
这是我获取坐标并将其转换的代码:
public Geometry getGeometryDistrict(String refCatDistrict, String shapeFileDistrict){
Geometry union = null;
File shapeFile = new File (shapeFileDistrict);
try {
Query query = new Query();
query.setCoordinateSystem(DefaultGeographicCRS.WGS84);
Filter filter = CQL.toFilter("obj_co_id = '"+refCatDistrict+"'");
query.setFilter(filter);
FileDataStore storeDistrict = FileDataStoreFinder.getDataStore(shapeFile);
SimpleFeatureSource featureSource = storeDistrict.getFeatureSource();
SimpleFeatureCollection collection = featureSource.getFeatures(query);
SimpleFeature fDistrict = null;
ArrayList<Geometry> geometries = new ArrayList<>();
SimpleFeatureIterator itrDistrict = collection.features();
while(itrDistrict.hasNext()){
fDistrict = itrDistrict.next();
Geometry geomDistrict = (Geometry)fDistrict.getDefaultGeometry();
geometries.add(geomDistrict);
}
GeometryFactory factory = new GeometryFactory();
GeometryCollection geometryCollection = (GeometryCollection) factory.buildGeometry( geometries );
union = geometryCollection.union();
itrDistrict.close();
storeDistrict.dispose();
}catch(IOException | NoSuchElementException | CQLException e){
Throwable cause = e.getCause();
System.out.println(cause);
}
return union;
}
我的问题是这些数据并不总是具有.prj文件(需要转换坐标),只有3个文件到达(.dbf,.shp和.shx)。
我的疑问是:如果没有.prj文件,是否可以将UTM坐标转换为WGS84?
答案 0 :(得分:1)
没有prj,您无法将UTM坐标转换为WGS84。您需要中央子午线或UTM区域进行转换。
但是有解决方法。您可以使用World UTM zone来预测UTM区域。如果您知道确切的区域,可以这样做。然后使用反复试验即可转换并检查附近的2-3个区域。
答案 1 :(得分:0)
我找到了这个解决方案,显然可以正常工作:
try{
CoordinateReferenceSystem sourceutm = CRS.decode(String.format("AUTO2:42001,%s,%s", -3.691406, 40.403431), true);
CoordinateReferenceSystem targetlatlong = CRS.decode("EPSG:4326", true);
MathTransform transform = CRS.findMathTransform(sourceutm, targetlatlong, false);
Geometry union = JTS.transform( geometryCollection, transform);
System.out.println(union.toString());
}catch(MismatchedDimensionException | FactoryException | TransformException e){
throw new RuntimeException(e);
}
这当然是一会儿。并向我返回UTM的度坐标。不需要.pjr。