how to project a flat image to spherical mercator EPGS:900013 to use in a Image Layer in openlayers

依然范特西╮ 提交于 2019-12-11 02:26:35

问题


I have a flat 400x400 pixels image which represents an area of 400x400 kilometers. I need to draw this image as an overlay on my OpenLayers mapping system. I use OpenStreetMap/Google Maps as base layers. I want to draw the overlay image using an Image Layer and I want to center my overlay image at a specific lat/lon point. The projection of the base layer is EPSG:900913 which is, in fact, the projection used by OpenStreetMap and Google Maps.

What I have understood (please correct me if I'm wrong), is that I can't use a flat, 'unprojected' image as an Image Layer, but I have to do a sort of 'reprojecting' to adjust the image to be compliant with the projection of the map, which is EPSG:900913. Searching here and the net, it seems I should use a library/tool called GDAL to do this, but whereas I am completely new to this I ask you:

  • Can anyone address me on how to use it?
  • Could you possibly tell me if what I have just said is correct and the way I described here is valid?
  • How could I use gdal to translate my flat unprojected 400x400 pixels image to an image projected in EPSG:900913, and suitable to be put as an Image Layer as an overlay?

Thanks.

I added the image as requested.

The answer provided by @capdragon was useful and let me going one step further. Anyway I have still some problems. I did this:

  1. used gdal_translate to set GCPs to the four corners and the center of the image like this:

gdal_translate -of "GTIFF" -gcp 0 0 8.6923 45.5427 -gcp 400 0 13.8149 45.5427 -gcp 200 200 11.2536 43.771702 -gcp 0 400 8.8413 41.9460 -gcp 400 400 13.6659 41.9460 quadrprod.png quadrprod_gpc.tif

This way I get a quadrprod_gcp.tif output. Then.

  1. used gdalwarp to apply the GCPs set in the point above and set the right projection, which is EPSG:4326 like this:

gdalwarp -t_srs EPSG:4326 quadrprod_gcp.tif quadrprod_gcp_warped.tif

This way I get a final quadrprod_gcp_warped.tif. The resulting image is no more 400x400 pixels but is 458x331 pixels. Then if I use gdalinfo to look at it, I found this:

gdalinfo quadrprod_gcp_warped.tif
Driver: GTiff/GeoTIFF
Files: quadrprod_gcp_warped.tif
Size is 458, 331
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (8.768046497499189,45.547291862003739)
Pixel Size = (0.010877474938832,-0.010877474938832)
Metadata:
    AREA_OR_POINT=Area
Image Structure Metadata:
    INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (   8.7680465,  45.5472919) (  8d46'4.97"E, 45d32'50.25"N)
Lower Left  (   8.7680465,  41.9468477) (  8d46'4.97"E, 41d56'48.65"N)
Upper Right (  13.7499300,  45.5472919) ( 13d44'59.75"E, 45d32'50.25"N)
Lower Right (  13.7499300,  41.9468477) ( 13d44'59.75"E, 41d56'48.65"N)
Center      (  11.2589883,  43.7470698) ( 11d15'32.36"E, 43d44'49.45"N)
Band 1 Block=458x4 Type=Byte, ColorInterp=Red
    Mask Flags: PER_DATASET ALPHA
Band 2 Block=458x4 Type=Byte, ColorInterp=Green
    Mask Flags: PER_DATASET ALPHA
Band 3 Block=458x4 Type=Byte, ColorInterp=Blue
    Mask Flags: PER_DATASET ALPHA
Band 4 Block=458x4 Type=Byte, ColorInterp=Alpha

As you can see the 4 corners and the center lat/lon are different from the values I set using -gcp values. My questions:

  • Why the lat/lon values returned by gdalinfo are different from the values I set in step 1 using -gpc?
  • Is it normal or am I doing something wrong?
  • What is next step? Should I translate the image again from EPSG:4326 to EPSG:900913?
  • When I place my image as overlay in openlayers, when I create the bounds object, should I use as southwest and northeast corners, the values I used in the -gcp or the final values I see with gdalinfo?

回答1:


What you're saying is correct.

You basically want to set some control points (reference points) using gdal_translate then warp the image using gdalwarp.

So for a rough projection you could simply use the corner cordinates to set 4 control points like so. (These examples are using EPSG:4326, but you can use EPSG:900913.

Set 4 control points (corner coordinates)

gdal_translate -a_srs EPSG:4326 -gcp 0 0 -89.38939600 30.39282800 -gcp 1024 0 -87.00029400 30.01043900 -gcp 0 1250 -89.99424800 27.37030800 -gcp 1024 1250 -87.67748400 26.98606100 "originalImage.tif" "image_trans.tiff"

The above is saying that pixel x=0 and y=0 is equal to "-89.38939600 30.39282800" pixel x=1024 and y=0 is -87.00029400 30.01043900 and so forth.

Warp the image:

gdalwarp -dstalpha -t_srs EPSG:4326 image_trans.tiff image_warped.tiff

If the warping isn't good enough you're going to need to set more control points on the image.

UPDATE

I downloaded your image and ran the commands you have. Everything worked fine for me: |

    captDragon@liberatecuba:/media/Data/tmp/orthoTesting$ gdal_translate -of "GTIFF" -gcp 0 0 8.6923 45.5427 -gcp 400 0 13.8149 45.5427 -gcp 200 200 11.2536 43.771702 -gcp 0 400 8.8413 41.9460 -gcp 400 400 13.6659 41.9460 quadrprod.png quadrprod_gpc.tif
    Input file size is 400, 400
    0...10...20...30...40...50...60...70...80...90...100 - done.
captDragon@liberatecuba:/media/Data/tmp/orthoTesting$ gdalinfo quadrprod_gpc.tif 
    Driver: GTiff/GeoTIFF
    Files: quadrprod_gpc.tif
    Size is 400, 400
    Coordinate System is `'
    GCP Projection = 
    GCP[  0]: Id=1, Info=
              (0,0) -> (8.6923,45.5427,0)
    GCP[  1]: Id=2, Info=
              (400,0) -> (13.8149,45.5427,0)
    GCP[  2]: Id=3, Info=
              (200,200) -> (11.2536,43.771702,0)
    GCP[  3]: Id=4, Info=
              (0,400) -> (8.8413,41.946,0)
    GCP[  4]: Id=5, Info=
              (400,400) -> (13.6659,41.946,0)
    Image Structure Metadata:
      INTERLEAVE=PIXEL
    Corner Coordinates:
    Upper Left  (    0.0,    0.0)
    Lower Left  (    0.0,  400.0)
    Upper Right (  400.0,    0.0)
    Lower Right (  400.0,  400.0)
    Center      (  200.0,  200.0)
    Band 1 Block=400x5 Type=Byte, ColorInterp=Red
      Mask Flags: PER_DATASET ALPHA 
    Band 2 Block=400x5 Type=Byte, ColorInterp=Green
      Mask Flags: PER_DATASET ALPHA 
    Band 3 Block=400x5 Type=Byte, ColorInterp=Blue
      Mask Flags: PER_DATASET ALPHA 
    Band 4 Block=400x5 Type=Byte, ColorInterp=Alpha


来源:https://stackoverflow.com/questions/18960229/how-to-project-a-flat-image-to-spherical-mercator-epgs900013-to-use-in-a-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!