Multiline and Polygon features use [lat, lon] but Single Line uses [lon, lat]

Tysona year ago

When I call the API and get my geofences, only the single lines were rendering.

Upon inspection of the geofence area property (wkt format), it looks like the single lines use [latitude, longitude] while polygons and multi-line features use [longitude, latitude]

Tysona year ago

I assume this is a known 'feature' since the legacy version of the web interface (OpenLayers version) has a GeofenceConverter.js which uses regex to parse the wkt text. I'm guessing that this is pretty much un-fixable without corrupting everyone's geofences.

Anton Tananaeva year ago

Just tested. Both types of geofences use [latitude, longitude] format.

Tysona year ago

Did you try drawing a line with only 1 segment? When the line is just a single segment it is definitely [longitude, latitude]. I tested on one of the demo servers. If you do a line with multiple segments it is [latitude, longitude] but a line with only 1 segment is [longitude, latitude]

Tysona year ago

Also, I think WKT specification says to use [longitude, latitude] format. When I try to read the [latitude, longitude] format with WKT in OpenLayers it doesn't work because it expects [long, lat]. Is there any way to get the geofences as GeoJSON format?

PS I think i mixed up latitude and longitude in my original post, but there is definitely a difference in how the single line segment geofences are formatted.

Anton Tananaeva year ago

Did you try drawing a line with only 1 segment?

No, I tried two or three segments.

I think WKT specification says to use [longitude, latitude] format

No, it does not. It is definitely more common, but the WKT standard doesn't actually specify the order.

Is there any way to get the geofences as GeoJSON format?

It should be easy to convert. We do it in the official web app. But there's no backend API for it.

Tysona year ago

Here is a way to transpose the Wkt coordinates to [lon, lat]

For polygons and multilines it will transpose, and for single line segments it will return the original string.

function transposeWkt(wktString) {
      const regex = /-?\d+\.\d+/g;
      const coordinatePairs = wktString.match(regex);
      const transposedPairs = [];

      for (let i = 0; i < coordinatePairs.length; i += 2) {
         const lon = coordinatePairs[i];
         const lat = coordinatePairs[i + 1];
         transposedPairs.push(`${lat} ${lon}`);
      }

      const coordPairs = transposedPairs.map((coord) => {
         const [lon, lat] = coord.split(" ");
         return `${lon} ${lat}`;
      });

      if (wktString.startsWith("POLYGON")) {
         //POLYGON
         return `POLYGON ((${coordPairs.join(", ")}, ${coordPairs[0]}))`;
      }
      if (coordPairs.length > 2) {
         //MULTI LINE
         return `LINESTRING (${coordPairs.join(", ")})`;
      } else {
         //SINGLE LINE
         return wktString;
      }
   }