Creating openlayers feature in the backend

56 views Asked by At

I have a website where I render a map using openlayers, I save the features (polygons) on the map to the database so I can load them later by doing this:

const format = new GeoJSON({ featureProjection: 'EPSG:3857' });
const json = format.writeFeature(feature);
const coordinatesToSave = JSON.stringify(JSON.parse(json).geometry.coordinates);

Which usually returns something like this:

  [
    [0.021913054136367526,-0.057815561863762355],
    [0.026207000014373007,-0.057815561863762355],
    [0.026207000014373007,-0.07047281626994106],
    [0.021913054136367526,-0.07047281626994106],
    [0.021913054136367526,-0.057815561863762355]
  ]
]

Now aslong as I create the features in the frontend, save them to the DB, then load them the next time this way:

const format = new GeoJSON({ featureProjection: 'EPSG:3857' });
const feature = format.readFeature(`{"type": "Feature","geometry": {  "type": "Polygon",  "coordinates": ${coordinates}},"properties": null}`);

It works fine, but what I'm trying to achieve is the following:

I have the map image with it's dimensions and extent, I need to generate the coordinates for a new feature from pixel coordinates of a polygon that I have,:

x1, y1, x2, y2 (all in pixels)

Using the above I need to convert or generate coordinates which openplayers will be able to read, on the backend, so I don’t really have openlayers functions to use

Thanks

0

There are 0 answers