routes - 是否可以将 HERE 灵活折线直接写入 Postgis?
问题描述
是否有可能直接将 HERE 灵活折线写入我的 Postgis/Postgres 数据库而不解码,转换为文本,然后使用 ST_MakeLine(ST_MakePoint(...) 制作几何?恐怕没有可能性(除了使用 Google 编码的折线)。或者有什么可以使用的吗?
背景:我正在测试 HERE-API 进行路由。我找到了从 Github 解码生成的折线的 Java 源:https ://github.com/heremaps/flexible-polyline 。因此我可以从这个 flexxible-polyline-String 中读取坐标
- 灵活折线:
BG8_9j7Cy11iXsBlBoBrEnBrErJrJzF3DnGvC7GnBvHoBjNkI7BrE7GvR7BjDrEvH_E3I_EvHvCrE3SnajNrTjIjNvHjN7G_OvHjSnGnQ_EzP_E_TrEnVrErYjDvWrEvgB7BnLjD_TvCjXjDvgB5D9qB
- 这导致坐标: (47.774718 / 12.102489) (47.77474 / 12.10247) (47.77476 / 12.1024) (47.77474 / 12.10233) ....(在德国南部)
- 或作为 Postgis 请求:
ST_GeomFromText('LINESTRING(12.102489 47.774718, 12.10247 47.77474, 12.1024 47.77476, 12.10233...
我的要求是:
但现在我想将此点列表转换为 Postgis。我希望使用现有的函数,例如来自Postgis的 ST_LineFromEncodedPolyline。不幸的是,这不会返回有效结果,因为它基于 Google 编码/解码算法。ST_LineFromEncodedPolyline 的结果是(清楚地表明这些折线压缩之间存在巨大差异):
SELECT ST_AsEWKT(ST_LineFromEncodedPolyline('BG8_9j7Cy11iXsBlBoBrEnBrErJr.....
--Result:
SRID=4326;LINESTRING(0.00004 -0.00002,0.0042 -0.00015,0.00422 -0.00405,0.00431 -0.00104,0.0048 ...
是否有某种方法,例如使用乘数从 HERE-String 获取此几何?
解决方案
由于我找不到答案或可行的解决方案,所以我写了我希望不要...我的结果如下:
Java 杰克逊解析器
ObjectMapper oJacksonObjectMapper = new ObjectMapper(); IOUtils.copy(oRespIputStream, oStringWriter, Charset.forName(StandardCharsets.UTF_8.name()) ); JsonNode oJsonNode = oJacksonObjectMapper.readTree(sJsonString);
RoutingHereSection.java
@JsonProperty("polyline") private String polylineString; private List<LatLngZ> listeLatLonCoords; private String listeLatLonCoordsAsPostgisString;
PolylineEncoderDecoder.java(来自 Github)添加方法:
public String getKoordinatenMitSpaceFuerPostgis() { return ""+ lng +" "+ lat; // => not lat/lng for Postgis }
解析坐标字符串
this.listeLatLonCoords = PolylineEncoderDecoder.decode( this.polylineString );
循环创建 LINESTRING
this.listeLatLonCoordsAsPostgisString = ""; if (this.getListeLatLonCoordsAnzahl()>0) { String sGeom = "LINESTRING("; for (int ii=0; ii<listeLatLonCoords.size();ii++) { if (ii>0) { sGeom +=", "; } sGeom += listeLatLonCoords.get(ii).getKoordinatenMitSpaceFuerPostgis(); } sGeom += ")"; this.listeLatLonCoordsAsPostgisString = sGeom; }
插入使用 Jdbc
String sSql = "INSERT INTO mytable ST_GeomFromText( ? ,4326) " Object[] oParam = new Object[] { oSectionReturn.getListeLatLonCoordsAsPostgisString() } int[] oType = new int[] { java.sql.Types.VARCHAR } getJdbcTemplate().execute( sSql )
就是这样……或多或少。
推荐阅读
- amazon-web-services - 如何为我的 Elastic Beanstalk Java 应用程序启用 HTTPS?
- javascript - Electron JS - 无法解构'require(...).remote'的属性'BrowserWindow',因为它未定义
- javascript - 导航回具有相同状态的搜索页面 (JavaScript)
- google-play - 如何从 Google Play 音乐转移到 Plex 音乐
- snowflake-cloud-data-platform - 从命名的外部阶段与外部位置加载文件
- python - 如何更改 RS4 类属性的默认值 - rpy2
- python - 如何通过列表值命名在 for 循环中开发的每个数据框
- python - 用 NaN 替换组的最后一个值
- c++ - C++创建一个函数的朋友?
- jquery - jQuery查找元素