Is it possible to write HERE flexible-polyline directly into Postgis?

Viewed 438

Is there a Possibility to directly write a HERE flexible-polyline into my Postgis/Postgres Database without Decoding, converting to Text and then make a Geometry using ST_MakeLine(ST_MakePoint(...) ? I am afraid that there is no possiblilty (other than with Google-encoded-Polyline). Or is there something to use?

Background: I am testing the HERE-API for routing. I have found the Java-source to Decode the resulting Polyline from Github: https://github.com/heremaps/flexible-polyline. Therefore I can read the coordinates from this flexxible-polyline-String

  • flexible-Polyline: BG8_9j7Cy11iXsBlBoBrEnBrErJrJzF3DnGvC7GnBvHoBjNkI7BrE7GvR7BjDrEvH_E3I_EvHvCrE3SnajNrTjIjNvHjN7G_OvHjSnGnQ_EzP_E_TrEnVrErYjDvWrEvgB7BnLjD_TvCjXjDvgB5D9qB
  • this results in the coordinates: (47.774718 / 12.102489) (47.77474 / 12.10247) (47.77476 / 12.1024) (47.77474 / 12.10233) .... (in the South of Germany)
  • or as Postgis-Request: ST_GeomFromText('LINESTRING(12.102489 47.774718, 12.10247 47.77474, 12.1024 47.77476, 12.10233...

My Request is:

But now I wanted to convert this Point-List to Postgis. I hoped to use an existing function, e.g. ST_LineFromEncodedPolyline from Postgis. Unfortunately this does not return valid results as it is based on the Google encoding/decoding algorythm. Results from ST_LineFromEncodedPolyline are (which clearly shows that it is a huge difference between these Polyline-compressions):

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 ...

Is there some way e.g. using a multiplier to get this geometry from the HERE-String?

1 Answers

As I couldn't find an answer or a working solution, I wrote as I hoped not to ... My results are the following:

  1. Java Jackson-Parser

    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 (from Github) added Method:

    public String getKoordinatenMitSpaceFuerPostgis() {
       return ""+ lng +" "+ lat;   // => not lat/lng for Postgis
    }
    
  4. parse the Coordinate-String

      this.listeLatLonCoords = PolylineEncoderDecoder.decode( this.polylineString );
    
  5. Loop to create 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. INSERT into using 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 )
    

that's it ... more or less.

Related