我有来自 Hibernate 实体的 GML 字符串,我想将其转换为 GML 对象。有没有像 GML 对象一样的东西,就像我们有几何对象一样?我想做的是在浏览器中打印到 GML,以便可以扩展其标签,但现在由于我将 GML 作为字符串,它只是将 GML 作为字符串打印
"gml": [
"<gml:Polygon>\n <gml:outerBoundaryIs>\n <gml:LinearRing>\n <gml:coordinates>\n -83.675395,36.540759 -83.675395,39.466012 -75.16643499999999,39.466012 -75.16643499999999,36.540759 -83.675395,36.540759 \n </gml:coordinates>\n </gml:LinearRing>\n </gml:outerBoundaryIs>\n</gml:Polygon>\n"]
我想打印 GML,以便可以扩展标签。
最佳答案
GeoTools 可以读取 GML v2 或 3 并为您提供 Hibernate 空间使用的 JTS 几何图形:
import org.geotools.gml3.GMLConfiguration;
import org.geotools.xml.Parser;
import com.vividsolutions.jts.geom.Geometry;
Reader reader = new StringReader(gmlString);
GMLConfiguration gmlConfiguration = new GMLConfiguration();
Parser gmlParser = new Parser(gmlConfiguration);
Geometry geometry = (Geometry) gmlParser.parse(reader);
这是another example from the GeoTools tests ,但它使用特征而不是几何。
然后使用 org.geotools.geojson.feature.FeatureJSON 或 org.geotools.geojson.geom.GeometryJSON获取 Geometry 实例并给出 JSON 字符串,如下所示:
GeometryJSON geometryJSON = new GeometryJSON();
String json = geometryJSON.toString(geometry)
我希望这足以让人们开始。好像代码示例不多。
关于java - Hibernate 序列化中的字符串到 GML 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14328296/