title: Converting image X,Y coordinates to longitude and latitude?
body: <p>I have set values of minimum longitude and latitude of a specific static map image. That map image is a cut of some country.</p>
  <pre><code>/**
   * Maximum longitude value of the map
   */
  private float mapLongitudeMax;
  
  /**
   * Minimum longitude value of the map
   */
  private float mapLongitudeMin;
  
  /**
   * Maximum latitude value of the map
   */
  private float mapLatitudeMax;
  
  /**
   * Minimum latitude value of the map
   */
  private float mapLatitudeMin;
  </code></pre>
  <p>And I have a <code>BufferedImage</code> called <code>mapImage</code>.</p>
  <p>I have a method that I wrote with a friend that receives <code>longitude</code> and <code>latitude</code> and gives you an <code>X</code> and a <code>Y</code> position approximately on the map so you can draw something on the map.</p>
  <p>Now if I want to move my mouse around the map, I want it to show <code>longitude/latitude</code> of my mouse position, that means I need to create a method which converts X and Y of the mouse position to <code>longitude</code> and <code>latitude</code>, which should do the opposite of my other method.</p>
  <p>This is my method to convert globe coordinates to image <code>X</code> and <code>Y</code>:</p>
  <pre><code>protected Location getCoordinatesByGlobe(float latitude, float longitude) {
  
      /**
       * Work out minimum and maximums, clamp inside map bounds
       */
      latitude = Math.max(mapLatitudeMin, Math.min(mapLatitudeMax, latitude));
      longitude = Math.max(mapLongitudeMin, Math.min(mapLongitudeMax, longitude));
  
      /**
       * We need the distance from 0 or minimum long/lat
       */
      float adjLon = longitude - mapLongitudeMin;
      float adjLat = latitude - mapLatitudeMin;
  
      float mapLongWidth = mapLongitudeMax - mapLongitudeMin;
      float mapLatHeight = mapLatitudeMax - mapLatitudeMin;
  
      float mapWidth = mapImage.getWidth();
      float mapHeight = mapImage.getHeight();
  
      float longPixelRatio = mapWidth / mapLongWidth;
      float latPixelRatio = mapHeight / mapLatHeight;
  
      int x = Math.round(adjLon * longPixelRatio) - 3;// these are offsets for the target icon that shows.. eedit laterrr @oz
      int y = Math.round(adjLat * latPixelRatio) + 3; //
  
      // turn it up
      y = (int) (mapHeight - y);
  
      return new Location(x, y);
  }
  </code></pre>
  <p>Now I tried thinking, the first thought that came into my head is just doing the same in reverse... so I started doing it and I ran into problems like, I can't get the value of <code>adjLon</code> or <code>adjLat</code> without having the <code>longitude</code> or <code>latitude</code>, so this can't be simply done by reversing it. I am all new to coordinates systems so it's all a bit confusing for me but I am starting to catch it up.</p>
  <p>Any tips for me?</p>
  <h2>EDIT (Its not possible?)</h2>
  <p>According to <a href="https://stackoverflow.com/a/16271669/3123545">this answer</a>, you can't really get real results because the earth is not flat, it can't really be converted to a flat map with longitude and latitude without implementing a real mathematical algorithm to make it work with the changes.</p>
  <p>There are few reasons in my code why the answer can not be exact:</p>
  <ol>
  <li>Because of the reason above</li>
  <li>Because my X,Y values are integers and not floats.</li>
  </ol>
  <p>So my question now, if it is really impossible with my method?</p>