PHP Map projections

前端 未结 2 1577
逝去的感伤
逝去的感伤 2021-01-17 05:01

I have Googled myself to death.. I am attempting to write 2 php functions that will return X and Y from Lat and Long, in both Mercator and flat non-projected (grid) maps. Pr

相关标签:
2条回答
  • 2021-01-17 05:14

    The proportional approach in the previous answer won't work. Mercator projections are quite non-linear.

    Here's how I overlay generated images onto a Google or Bing map. In my case, I'm creating a GD image of polygons that will be the overlay. It's much faster to do the polygons in the GD library than the map providers APIs.

    First, set up scaling from a standard latitude longitude to a WGS84 projection. Degrees to mercator x-y coordinates in meters.

    http://gisgeography.com/wgs84-world-geodetic-system/

    // $minlat = minimum image latitude

    // $minlon = minimum image longitude

    // $maxlat = maximum image latitude

    // $maxlon = maximum image longitude

    // $latbounds = Image height (in pixels)

    // $lonbounds = Image width (in pixels)

    $lonrange = abs($maxlon - $minlon);
    $WGS84min = log(tan((90.+$minlat)*M_PI/360.))/(M_PI/180.);
    $WGS84min = (int) ($WGS84min * 2037598.34/180);
    $WGS84max = log(tan((90.+$maxlat)*M_PI/360.))/(M_PI/180.);
    $WGS84max = (int) ($WGS84max * 2037598.34/180);
    $WGS84diff = $WGS84max - $WGS84min;
    $WGS84factor = $latbounds/$WGS84diff;
    

    Then for each latitude/longitude I want to calculate the actual X-Y coordinates on the image.

    // $lon1 = the longitude of the point to be converted into image coordinates

    // $lat1 = the latitude of the point to be converted into image coordates

    X is easy

    $x = (int) ((abs($lon1-$minlon)/$lonrange)*$lonbounds);
    

    Y is a bit harder, first calculating to WGS84, and then mapping to the image. Last step, inverting the Y coordinates since the display order is upside down.

    $y1 = log(tan((90.+$lat1)*M_PI/360.))/(M_PI/180.);
    $y1 = $y1 * 2037598.34/180;
    $y1 = (int) (($y1- $WGS84min)*$WGS84factor);
    $y  = $latbounds - $y1;
    

    when the image file is complete, use GD to save the image and then use the example in the API library to display your overlay.

    https://developers.google.com/maps/documentation/javascript/examples/overlay-simple

    0 讨论(0)
  • 2021-01-17 05:32

    This presumes your lat/long coords are decimal values, and don't change direction from north/south or east/west within the visible range of the map. If they do, such values should be kept to a single format, and made negativel (eg: 1.0 S would become -1.0 N)

    First you'd set the following variables, either finding them with PHP or if you know them already in the script:

    $width=//width of image
    $height=//height of image
    
    $long_at_left=//Longitude of left-hand coordinates
    $long_at_right=//Longitude of right-hand coordinates
    $lat_at_left=//Latitude of left-hand coordinates
    $lat_at_right=//Latitude of right-hand coordinates
    
    $target_long=//Longitude you want to find
    $target_lat=//Latitude you want to find
    

    Then use:

    $xtarget=$target_long-$long_at_left;
    $ytarget=$target_lat-$lat_at_top;
    
    $xdist=$long_at_left-$long_at_right;
    $ydist=$lat_at_top-$lat_at_bottom;
    
    $x=round(($xtarget/$xdist)*$width); //Percentage of distance times width
    $y=round(($ytarget/$ydist)*$height); //Percentage of distance times height
    

    Or something of that form should do the trick.

    0 讨论(0)
提交回复
热议问题