Search code examples
javageometryopenlayers

How to find the position correspondence of a point in 2 rectangular selections with different position


That's the situation, I've 3 kind of different coordinates, the rectangleOld,rectangleNew and pointIntoRectangleOld so ->

rectangleOld = [
  [-23374.69715123405, 6706811.7857385],
  [-23516.832747730223, 6706811.7857385],
  [-23516.832747730223, 6706948.18295946],
  [-23374.69715123405, 6706948.18295946],
  [-23374.69715123405, 6706811.7857385]
];

pointIntoRectangleOld = [-23440.522912441458,6706879.939226517];

rectangleNew = [
  [-22869.755094112752, 6706693.921604385],
  [-23011.890690608925, 6706693.921604385],
  [-23011.890690608925, 6706830.318825345],
  [-22869.755094112752, 6706830.318825345],
  [-22869.755094112752, 6706693.921604385],
]

Given this information, how can I calculate where should be the position of a new point in the new rectangle that has the exact same position as the first one in the old rectangle?

I'm developing in java, and I've trivially tried this, but it doesn't seem to work:

double lat1=Double.parseDouble(coordinateOldx);
double lat2=Double.parseDouble(plantPosX);
double xPosPlant=lat1-lat2;
double long1=Double.parseDouble(coordinateOldy);
double long2=Double.parseDouble(plantPosY);
double yPosPlant=long1-long2;
System.out.println(xPosPlant);
System.out.println(yPosPlant);
            
double latnew=Double.parseDouble(coordinateNewx);
latnew=latnew + xPosPlant;
double longNew=Double.parseDouble(coordinateNewy);
longNew=longNew + yPosPlant;

latNew and longNew should be the coordinate x/y of the point, i tried starting from a single angle of the new and old rectangle but is not working.


Solution

  • There are some peculiarities for "rectangles" at the sphere, but you can do the next:

    Calculate relative position of point in the first rectangle.
    Recalculate this relative position into absolute coordinates int he second rectangle.

    To get (u,v) coordinates (range 0..1) in the first rectangle, use opposite corners

     u = (plant.lat - rectOld[0].lat) / (rectOld[2].lat - rectOld[0].lat)
     v = (plant.lon - rectOld[0].lon) / (rectOld[2].lon - rectOld[0].lon)
    

    And for new coordinates

    plantNew.lat = rectNew[0].lat + u * (rectNew[2].lat - rectNew[0].lat)
    plantNew.lon = rectNew[0].lon + v * (rectNew[2].lon - rectNew[0].lon)