Screen coordinates and CRS coordinates

When working with geospatial applications, a common operation is to convert between screen pixel coordinates and CRS (coordinate reference system) coordinates, describing real-world locations. This can be useful in various cases, such as displaying the geographical coordinate for the mouse cursor position, working with custom tools for user interaction, editing feature geometries from code, and more.

Transforming coordinates from screen pixel coordinates to the CRS of a specific DataSet is very easy in Carmenta Engine, but it’s important to remember that it must go through the CRS of the View as an intermediary step, because the View is what is drawn on screen.

With Carmenta Engine, it is very easy to perform these two conversion steps, thanks to these utility methods:

  • The methods PixelToCrs and CrsToPixel on the View class allow converting coordinates from pixel to the View CRS, and vice-versa.
  • The method ProjectTo on the Crs class allows converting coordinates between any two CRSs.

In practice, this is how we can get point coordinates in the DataSet CRS from the pixel coordinates at position (x, y):

Point pixelCoordinates = new Point(x, y);

Point viewCrsCoordinates = myView.PixelToCrs(pixelCoordinates);

Point datasetCrsCoordinates = myView.Crs.ProjectTo(myDataSet.Crs, viewCrsCoordinates);
Point pixelCoordinates(x, y);

Point viewCrsCoordinates = myView->pixelToCrs(pixelCoordinates);

Point datasetCrsCoordinates = myView->crs()->projectTo(myDataSet->crs(), viewCrsCoordinates);

Which can be summarized into a single line:

Point datasetCrsCoordinates = myView.Crs.ProjectTo(myDataSet.Crs, myView.PixelToCrs(x, y))
Point datasetCrsCoordinates = myView->crs()->projectTo(myDataSet->crs(), myView->pixelToCrs(x, y));

Easy and useful! To go deeper into geographical coordinates, check out these resources: