首页 > 解决方案 > 是否可以将 HERE 灵活折线直接写入 Postgis?

问题描述

是否有可能直接将 HERE 灵活折线写入我的 Postgis/Postgres 数据库而不解码,转换为文本,然后使用 ST_MakeLine(ST_MakePoint(...) 制作几何?恐怕没有可能性(除了使用 Google 编码的折线)。或者有什么可以使用的吗?

背景:我正在测试 HERE-API 进行路由。我找到了从 Github 解码生成的折线的 Java 源:https ://github.com/heremaps/flexible-polyline 。因此我可以从这个 flexxible-polyline-String 中读取坐标

我的要求是:

但现在我想将此点列表转换为 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 获取此几何?

标签: routespostgishere-api

解决方案


由于我找不到答案或可行的解决方案,所以我写了我希望不要...我的结果如下:

  1. Java 杰克逊解析器

    ObjectMapper oJacksonObjectMapper = new ObjectMapper();
    IOUtils.copy(oRespIputStream, oStringWriter, Charset.forName(StandardCharsets.UTF_8.name()) );
    JsonNode oJsonNode = oJacksonObjectMapper.readTree(sJsonString);
    
  2. RoutingHereSection.java

    @JsonProperty("polyline")
    private String polylineString;
    private List<LatLngZ> listeLatLonCoords; 
    private String listeLatLonCoordsAsPostgisString;
    
  3. PolylineEncoderDecoder.java(来自 Github)添加方法:

    public String getKoordinatenMitSpaceFuerPostgis() {
       return ""+ lng +" "+ lat;   // => not lat/lng for Postgis
    }
    
  4. 解析坐标字符串

      this.listeLatLonCoords = PolylineEncoderDecoder.decode( this.polylineString );
    
  5. 循环创建 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;
            }
    
  6. 插入使用 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 )
    

就是这样……或多或少。


推荐阅读