我在GeoTools的帮助下遇到了一个关于心爱坐标变换的问题:我想将Gauss-Kruger(第5区,EPSG 31469)的一组坐标转换成普通的WGS84坐标(EPSG 4326).
我用一个简单的例子构建了一个代码(只需要一对坐标):
double coordX = 5408301.53;
double coordY = 5659230.5;
double[] punt = new double[2];
CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:31469");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
DirectPosition expPt = new GeneralDirectPosition(coordX, coordY);
expPt = transform.transform(expPt, null);
punt = expPt.getCoordinate();
System.out.println(punt[0] + ", " + punt[1]); //lon, lat
Run Code Online (Sandbox Code Playgroud)
调试后的结果如下:48.791886921764345,17.16525096311777
当我检查我获得的WGS84坐标(只是将它们打到谷歌地图)时,我最终在奥地利附近的捷克共和国的某个地方,虽然这对坐标应该在德国东部的某个地方(当然,我用一些html检查了它)解码器):
--->应该是结果:51.0609167,13.6900142.
我无法想象出现这种失败的原因.GeoTools获得了两个想要的CRS(我从java控制台中附加了响应的摘录)
有人能解释一下吗?我很感激任何帮助!
许多问候,塞巴斯蒂安
**sourceCRS:**
PROJCS["DHDN / 3-degree Gauss-Kruger zone 5",
GEOGCS["DHDN",
DATUM["Deutsches Hauptdreiecksnetz",
SPHEROID["Bessel 1841", 6377397.155, 299.1528128, AUTHORITY["EPSG","7004"]],
TOWGS84[612.4, 77.0, 440.2, -0.054, 0.057, -2.797, 2.55], …Run Code Online (Sandbox Code Playgroud) 问题:
我有一个包含目标区域(黄色)的形状文件.
我有一个包含建筑物的绿色文件(绿色).
我需要黄色区域的白色空间.
picture :: //db.tt/kjjXZlQF
我的解决方案
获取该地区的所有建筑物
Filter inPolygon = CQL.toFilter("WITHIN(the_geom,"+wktwriter.write(targetarea) + ")");
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures(inPolygon);//this works
从每个建筑物中获取它的几何形状并在目标区域使用差异
toCover = toCover.Difference(building);
或第二解决方案:
每个建筑物的联盟然后差异到底.
或第三解决方案:
将它们全部放在GeomtryCollection调用union中,然后使用差异
这些解决方案的每个人都给我一些类似下图的内容
图片:https://dl.dropboxusercontent.com/u/639458/stackoverflow/stfr2.png
很明显,我尝试了几种方法来解决这个问题,通过使用不同的方式创建/制作或使用它.即使使用下面站点中的给定代码,它也无法正常工作.
http://docs.geotools.org/latest/userguide/library/jts/combine.html
这里有任何Geotools开发人员吗?我们发现了GeometryJSON的以下奇怪行为:
Geometry geom = getGeometry();
System.out.println(geom);
GeometryJSON g = new GeometryJSON();
StringWriter sw = new StringWriter();
try {
g.write(geom, sw);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sw.toString());
Run Code Online (Sandbox Code Playgroud)
输出:
POLYGON((1.1212121214354352354235235423521 2.1212121121,4.454545454545445 3.454544545454454,10.515545445454 20.1545454654664, 1.1212121214354352354235235423521 2.1212121121))
{"type":"Polygon","coordinates":[[[1.1212,2.1212],[4.4545,3.4545],[10.5155,20.1545],[1.1212,2.1212]]]}
Run Code Online (Sandbox Code Playgroud)
多边形坐标是圆形的.这是有意的吗?
我正在使用JTS(来自VividSolutions)和GeoTools。我有以下代码:
public Geometry jsonToGeom(String json) throws IOException {
Geometry obj = new GeometryJSON().read(json);
return obj;
}
Run Code Online (Sandbox Code Playgroud)
但是,这将返回以下RunTimeException:
java.lang.RuntimeException: java.lang.NoSuchMethodException: org.geotools.geojson.feature.FeatureHandler.<init>(com.vividsolutions.jts.geom.GeometryFactory)
at org.geotools.geojson.DelegatingHandler.createDelegate(DelegatingHandler.java:130)
at org.geotools.geojson.geom.GeometryHandler.primitive(GeometryHandler.java:68)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.geotools.geojson.GeoJSONUtil.parse(GeoJSONUtil.java:236)
at org.geotools.geojson.geom.GeometryJSON.parse(GeometryJSON.java:655)
at org.geotools.geojson.geom.GeometryJSON.read(GeometryJSON.java:196)
at am.abhi.experiments.geotoolstest.GeoJson.jsonToGeom(GeoJson.java:13)
at am.abhi.experiments.geotoolstest.SomeTest.testSomething(SomeTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) …Run Code Online (Sandbox Code Playgroud) 例如,如果您有一个带有多边形的GeoJSON文件(用于测试的简单文件)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-4.658203125,
41.343824581185686
],
[
-5.6689453125,
39.13006024213511
],
[
-1.9335937499999998,
39.16414104768742
],
[
-1.3623046875,
41.21172151054787
],
[
-4.658203125,
41.343824581185686
]
]
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
要点:
Geometry point2 = new WKTReader().read("POINT (-3.2958984375 40.44694705960048)");
Run Code Online (Sandbox Code Playgroud)
并且您想在程序中加载geoJSON文件以测试该多边形是否包含该点,那么如何使用JTS在Java中进行处理呢?
其他选项可以使用带有GeoJson插件的GeoTools,但是我无法解析GeoJson文件
我尝试过的
像这样使用GEOTOOLS
String content = new String(Files.readAllBytes(Paths.get("file.geojson")), "UTF-8");
GeometryJSON gjson = new GeometryJSON();
Reader reader = new StringReader(content);
Polygon p = gjson.readPolygon(reader); …Run Code Online (Sandbox Code Playgroud) 所以我正在编写一个Java应用程序,我有一个ESRI Shapefile,其中包含所有美国州的边界.我需要的是能够确定任何给定的纬度/经度点是否在距离任何状态边界线的指定距离内 - 即,我不会指定特定的边界线,只需要查看该点是否接近任何一个他们
解决方案根本不必非常精确; 例如,我不需要处理垂直于边界的测量,或者其他什么.只是检查是否向北,向南,向东或向西X米将导致越过边界将是绰绰有余.解决方案必须具有计算效率,因为我将执行大量的这些计算.
我打算使用Shapefile插件来使用GeoTools库(尽管有一个更简单的选项,我都是为了它).我真正理解的是:一旦我将shapefile加载到内存中,我该如何检查我是否接近边界?
谢谢!-担
我使用此代码将具有单个功能的kml文件转换为GeoJson文件。
String kmlToGeoJson(String fileName)
throws IOException, ParserConfigurationException, SAXException, XMLStreamException {
FileInputStream reader = new FileInputStream(fileName);
PullParser parser = new PullParser(new KMLConfiguration(),reader, SimpleFeature.class);
FeatureJSON fjson = new FeatureJSON();
FileWriter tmp = new FileWriter(fileName + ".geojson");
BufferedWriter writer = new BufferedWriter(tmp);
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
while (simpleFeature != null) {
fjson.writeFeature(simpleFeature, writer);
simpleFeature = (SimpleFeature) parser.parse();
}
return "success";
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用具有多种功能的Kml文件时:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>KmlFile</name>
<Style id="west_campus_style">
<IconStyle>
<Icon>
<href>https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png
</href>
</Icon>
</IconStyle>
<BalloonStyle>
<text>$[video]</text>
</BalloonStyle>
</Style>
<Placemark> …Run Code Online (Sandbox Code Playgroud) 我正在通过添加新的列属性来转换 shapefile。由于此任务是使用 Java 执行的,因此我目前知道的唯一选择是使用 Geotools。我有两个主要问题:
1. 我不知道如何实际添加新的列变量。feature.setAttribute("col","value") 是答案吗?
我从这篇文章中看到的只是示例:https : //gis.stackexchange.com/questions/215660/modifying-feature-attributes-of-a-shapefile-in-geotools,但我没有得到解决方案。
//Upload the ShapeFile
File file = JFileDataStoreChooser.showOpenFile("shp", null);
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
DataStore store = DataStoreFinder.getDataStore(params);
SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
String typeName = store.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source =
store.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE;
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
try (FeatureIterator<SimpleFeature> features = collection.features()) {
while (features.hasNext()) {
SimpleFeature feature = features.next();
//adding new columns
feature.setAttribute("ShapeID", "SHP1213");
feature.setAttribute("UserName", "John");
System.out.print(feature.getID());
System.out.print(":");
System.out.println(feature.getDefaultGeometryProperty().getValue());
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建规则的六边形多边形,但是当在地图上可视化时,它们会垂直拉伸。
我就是这样做的:
List<Geometry> hexagons = new ArrayList<>();
CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;
ReferencedEnvelope gridBounds = new ReferencedEnvelope(box[0], box[2], box[1], box[3], sourceCRS);
double sideLen = 0.1;
GridFeatureBuilder builder = new DefaultGridFeatureBuilder();
SimpleFeatureSource grid = Hexagons.createGrid(gridBounds, sideLen, HexagonOrientation.FLAT, builder);
try{
SimpleFeatureCollection collection = grid.getFeatures();
FeatureIterator iterator = collection.features();
while (iterator.hasNext()){
Feature feature = iterator.next();
SimpleFeature sFeature = (SimpleFeature) feature;
Geometry geometry = (Geometry) sFeature.getAttribute(0);
hexagons.add(geometry);
}
} catch(Exception e){
e.printStackTrace();
}
return hexagons;
Run Code Online (Sandbox Code Playgroud)

就是它在地图上的样子。我尝试将方向从“平面”更改为“角度”,但这并没有解决问题。
我们正在将我们的项目迁移到 Java 9,并且在使用 geotools 时出现此异常:
java.lang.IllegalArgumentException: org.opengis.referencing.datum.DatumFactory is not an ImageIO SPI class
at java.desktop/javax.imageio.spi.ServiceRegistry.checkClassAllowed(ServiceRegistry.java:745)
at java.desktop/javax.imageio.spi.ServiceRegistry.<init>(ServiceRegistry.java:140)
at org.geotools.factory.FactoryRegistry.<init>(FactoryRegistry.java:155)
at org.geotools.factory.FactoryRegistry.<init>(FactoryRegistry.java:146)
at org.geotools.factory.FactoryCreator.<init>(FactoryCreator.java:82)
Run Code Online (Sandbox Code Playgroud)
我的Java版本:
openjdk 9
OpenJDK Runtime Environment (build 9+181)
OpenJDK 64-Bit Server VM (build 9+181, mixed mode)
Run Code Online (Sandbox Code Playgroud)
我知道在GeoTools 19.0 中有一些支持 java 9 的更改,但是将 geotools 升级到版本 19.0(以及 20.x!)并不能解决这个问题。
geotools ×10
java ×9
jts ×3
geojson ×2
gis ×2
attributes ×1
coordinates ×1
geography ×1
geospatial ×1
kml ×1
maps ×1
postgis ×1
shapefile ×1
wgs84 ×1