Use a point wkt as a circle with openlayers

935 Views Asked by At

I am looking for a way to draw circles within the WKT format using Openlayers. I know that the WKT standard doesn’t support circles but someone said that you might be able to use a point wkt and then set a radius for it (for android but might work for other things as well). my question is thus how do I do that in Openlayers if it is possible?

link to what Tom said https://stackoverflow.com/a/58430532

This is how I made a polygon

     let polyFeature = new ol.format.WKT().readFeature(polygonWKT, {
    // Must use both projections in order to draw the feature with the wkt format
    dataProjection   : "EPSG:4326",
    featureProjection: map.getView().getProjection() // Can at least get the standard projection and not have to fiddle with that
  });
  vectorSource.addFeature(polyFeature);

I’m asking this question to see if I can make the drawing/saving of the coordinates simpler. Right now, I have the coordinates in a string “coord1, coord2;” and have to split them when using a polygon and then convert them back into that string format when saving the coordinates. With wkt I can just throw the string into the Openlayers function and I’m done so if I can kind of do the same with the circle then that would be great.

What I use Openlayers v6 (some version of that) vanilla js, php

1

There are 1 best solutions below

2
On BEST ANSWER

If you always wrap the wkt in a JSON

'{"wkt":"POINT(28.625360369528934 77.2227479486792)"}'

You could optionally add a radius to indicate a circle

'{"wkt":"POINT(28.625360369528934 77.2227479486792)","radius":50}'

Then OpenLayers could parse the JSON string and convert a point with a radis to a circle

let json = JSON.parse(jsonString);
let polyFeature = new ol.format.WKT().readFeature(json.wkt, {
    dataProjection   : "EPSG:4326",
    featureProjection: map.getView().getProjection()
});
if (json.radius && polyFeature.getGeometry().getType() === 'Point') {
    polyFeature.setGeometry(new ol.geom.Circle(polyFeature.getGeometry().getCoordinates(), json.radius));
}
vectorSource.addFeature(polyFeature);