Building a Mercator Image from Un-projected data.. Major confusion and help requested.

8 messages Options
Embed this post
Permalink
Cassanova, Bill

Building a Mercator Image from Un-projected data.. Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Building a Mercator Image from Un-projected data.. Major confusion and help requested.

Hi all,

I am looking to gain a better understanding of how I can use GDAL to build a Mercator Image from un-projected data.

The data I have is contained in proprietary format.  For all intensive purposes it is a grid consisting of 2464 rows by 5796 columns of data.

The origin of the data is in the upper left hand corner of the grid with grid lat-lon dimensions of -125.99, 49.7186 for the upper left and

-65.656, 24.0938 for the lower right.

I have tried several things but not sure I get the connection well enough to complete the project. SoHere is my sample program with

Excerpts for comments.

std::pair< double, double > upper_left = std::make_pair( -125.99, 49.7186 );

std::pair< double, double > upper_right = std::make_pair( -65.656, 49.7186 );

std::pair< double, double > lower_left = std::make_pair( -125.99, 24.0938 );

std::pair< double, double > lower_right = std::make_pair( -65.656, 49.7186 );

int my_points = 1;

int result = 0;

double x, y;

OGRSpatialReferenceH mercator_proj = OSRNewSpatialReference( "" );

OSRSetProjCS( mercator_proj, "Mercator Projection" );

OSRImportFromProj4( mercator_proj, "+proj=merc" );

OGRSpatialReferenceH wgs84_proj = OSRNewSpatialReference( "" );

OSRSetWellKnownGeogCS( wgs84_proj,  "WGS84" );


OGRCoordinateTransformationH my_transformation = OCTNewCoordinateTransformation( wgs84_proj, mercator_proj );

x = upper_left.first;

y = upper_left.second;

OCTTransform( my_transformation, my_points, &x, &y, NULL );

std::cout << std::fixed;

std::cout << result << " " << upper_left.first << ", " << upper_left.second << " = " << x << ", " << y << std::endl;

The output for my example is:

0 -125.990000, 49.718600 = -14025142.645045, 6365068.664114

The values output in meters and are exactly what I would expect and are similar to proj4 output. 

My question is how do I convert this into actual i-j coordinates for the imageAnd even more so how do I convert these to i-j coordinates

For the data since it is un-projected?

Another Method is using GDALCreateGenImgProfTransformer:

char *pszDstWKT = NULL;

char *pszSrcWKT = NULL;

OSRExportToWkt( mercator_proj, &pszDstWKT );

OSRExportToWkt( wgs84_proj, &pszSrcWKT );


void *hTransformArg;

hTransformArg = GDALCreateGenImgProjTransformer( NULL, pszSrcWKT, NULL, pszDstWKT, FALSE, 0, 1 );

x = upper_left.first;

y = upper_left.second;

int success;

result = GDALGenImgProjTransform(hTransformArg, TRUE, 1, &x, &y,

                              NULL, &success);


std::cout << std::fixed;

td::cout << result << " " << upper_left.first << ", " << upper_left.second << " = " << x << ", " << y << std::endl;

// 1 -125.990000, 49.718600 = -0.001132, 0.000450

// OK, this seems sensible but is this the i-j offset into the data or the image or both

x = lower_right.first;

y = lower_right.second;


result = GDALGenImgProjTransform(hTransformArg, TRUE, 1, &x, &y,

                              NULL, &success);


std::cout << std::fixed;

std::cout << result << " " << lower_right.first << ", " << lower_right.second << " = " << x << ", " << y << std::endl;

// 1 -65.656000, 49.718600 = -0.000590, 0.000450

// This is NOT Right so apparently I am doing something wrong or not understanding the concept here

Thanks,

Bill

 


_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Christopher Barker

Re: Building a Mercator Image from Un-projected data.. Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
Cassanova, Bill wrote:
> I am looking to gain a better understanding of how I can use GDAL to
> build a Mercator Image from un-projected data.

Do you need to write your own code for this?, or could you just use the
  gdalwarp utility? (maybe after converting to a format gdal knows).

If you do need your own code, you might look at the gdalwarp code.

Which brings up a question I could answer by reading the source, but
since I'm here:

Is the core warping functionality in the GDAL API (and wrapped for
Python?), or in the gdalwarp code -- I may need to do some warping in
code as well.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Cassanova, Bill

RE: Building a Mercator Image from Un-projected data..Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)

 

Well, I sorta tried that:

 

gdalwarp -s_src '+proj=WGS84' -t_srs '+proj=merc' hiradff_200910121800_f180_weather_radar.tiff foo.tiff

 

And the reply is:

 

Usage: gdalwarp [--help-general] [--formats]

    [-s_srs srs_def] [-t_srs srs_def] [-to "NAME=VALUE"]

    [-order n] [-tps] [-rpc] [-geoloc] [-et err_threshold]

    [-te xmin ymin xmax ymax] [-tr xres yres] [-ts width height]

    [-wo "NAME=VALUE"] [-ot Byte/Int16/...] [-wt Byte/Int16]

    [-srcnodata "value [value...]"] [-dstnodata "value [value...]"] -dstalpha

    [-r resampling_method] [-wm memory_in_mb] [-multi] [-q]

    [-cutline datasource] [-cl layer] [-cwhere expression]

    [-csql statement] [-cblend dist_in_pixels]

    [-of format] [-co "NAME=VALUE"]*

    srcfile* dstfile

 

Available resampling methods:

    near (default), bilinear, cubic, cubicspline, lanczos.

 

The file is something GDAL should understand:

 

[vizuser@vizintqa exploded_visualizer]$ gdalinfo hiradff_200910121800_f180_weather_radar.tiff

Driver: GTiff/GeoTIFF

Files: hiradff_200910121800_f180_weather_radar.tiff

Size is 5796, 2464

Coordinate System is:

GEOGCS["Coordinate System imported from GRIB file",

    DATUM["unknown",

        SPHEROID["unnamed",6367470,0]],

    PRIMEM["Greenwich",0],

    UNIT["degree",0.0174532925199433]]

Origin = (234.010419999999982,49.706487000000003)

Pixel Size = (0.010399000000000,-0.010399000000000)

Metadata:

  AREA_OR_POINT=Area

Image Structure Metadata:

  INTERLEAVE=PIXEL

Corner Coordinates:

Upper Left  (     234.010,      49.706) (234d 0'37.51"E, 49d42'23.35"N)

Lower Left  (     234.010,      24.083) (234d 0'37.51"E, 24d 5'0.06"N)

Upper Right (     294.283,      49.706) (294d16'58.89"E, 49d42'23.35"N)

Lower Right (     294.283,      24.083) (294d16'58.89"E, 24d 5'0.06"N)

Center      (     264.147,      36.895) (264d 8'48.20"E, 36d53'41.71"N)

Band 1 Block=5796x1 Type=Byte, ColorInterp=Red

  Mask Flags: PER_DATASET ALPHA

Band 2 Block=5796x1 Type=Byte, ColorInterp=Green

  Mask Flags: PER_DATASET ALPHA

Band 3 Block=5796x1 Type=Byte, ColorInterp=Blue

  Mask Flags: PER_DATASET ALPHA

Band 4 Block=5796x1 Type=Byte, ColorInterp=Alpha

 

 

Am I missing something here?  I would however prefer to do it with code so that I can go directly from my data to an image without the intermediate step

Of using gdalwarp.

 

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Christopher Barker
Sent: Monday, November 02, 2009 4:39 PM
To: [hidden email]
Subject: Re: [gdal-dev] Building a Mercator Image from Un-projected data..Major confusion and help requested.

 

Cassanova, Bill wrote:

> I am looking to gain a better understanding of how I can use GDAL to

> build a Mercator Image from un-projected data.

 

Do you need to write your own code for this?, or could you just use the

  gdalwarp utility? (maybe after converting to a format gdal knows).

 

If you do need your own code, you might look at the gdalwarp code.

 

Which brings up a question I could answer by reading the source, but

since I'm here:

 

Is the core warping functionality in the GDAL API (and wrapped for

Python?), or in the gdalwarp code -- I may need to do some warping in

code as well.

 

-Chris

 

 

 

--

Christopher Barker, Ph.D.

Oceanographer

 

Emergency Response Division

NOAA/NOS/OR&R            (206) 526-6959   voice

7600 Sand Point Way NE   (206) 526-6329   fax

Seattle, WA  98115       (206) 526-6317   main reception

 

[hidden email]

_______________________________________________

gdal-dev mailing list

[hidden email]

http://lists.osgeo.org/mailman/listinfo/gdal-dev


_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Adam Nowacki

Re: Building a Mercator Image from Un-projected data.. Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
In reply to this post by Cassanova, Bill
Cassanova, Bill wrote:

> result = GDALGenImgProjTransform(hTransformArg, TRUE, 1, &x, &y,
>                               NULL, &success);
> std::cout << std::fixed;
> std::cout << result << " " << lower_right.first << ", " <<
> lower_right.second << " = " << x << ", " << y << std::endl;
>
> *//** 1 -65.656000, 49.718600 = -0.000590, 0.000450***
>
> *// This is NOT Right** so apparently I am doing something wrong or not
> understanding the concept here*

2nd argument to GDALGenImgProjTransform is TRUE so you are reprojecting
from, not to mercator
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Adam Nowacki

Re: Building a Mercator Image from Un-projected data..Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
In reply to this post by Cassanova, Bill
Cassanova, Bill wrote:
> *Well, I sorta tried that:*
> gdalwarp -s_src '+proj=WGS84' -t_srs '+proj=merc'
> hiradff_200910121800_f180_weather_radar.tiff foo.tiff

-s_srs not -s_src
Please, before posting on this list, at least check for typos!
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Cassanova, Bill

RE: Building a Mercator Image from Un-projected data..Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
In reply to this post by Adam Nowacki
Some javascript/style in this post has been disabled (why?)
Re: [gdal-dev] Building a Mercator Image from Un-projected data..Major confusion and help requested.
Thanks for the guidance.    The results are correct in the sense that the transformations are correct in terms of the meters values being returned.  My question is how does this convert to pixel location FROM the data set and then INTO the image.
In the below code example the bolded values are my output.  My lack of understanding comes from how do I use this information returned in Meters to navigate into my dataset and then navigate into the image?  Is it as simple of determining the meters
for the upper left and then meters for the lower right and then setting up a ratio based on the rows and columns in the input data set and corresponding output image?  This doesn't seem like a linear problem but perhaps GDAL is taking this out of the equation.
The output image will be the same number of rows and columns as the source data so no interpolation of data is needed.  My presumption is that the difference will be WHERE a particular pixel is located in the image.  Doing a straight Platte Carre
image is simple because the origin in the datasource is the same as the origin in the image.  The lowest right data element in the datasource will have the same i-j as the lowest right pixel.  My presumption and perhaps this is where I need clarification
is this will NOT be the case with Mercator or any other projection.

std::pair< double, double > upper_left = std::make_pair( -125.99, 49.7186 );

std::pair< double, double > upper_right = std::make_pair( -65.656, 49.7186 );

std::pair< double, double > lower_left = std::make_pair( -125.99, 24.0938 );

std::pair< double, double > lower_right = std::make_pair( -65.656, 24.0938 );

int my_points = 1;

int result = 0;

double x, y;

x = lower_right.first;

y = lower_right.second;

OCTTransform( my_transformation, my_points, &x, &y, NULL );

std::cout << std::fixed;

std::cout << result << " " << lower_right.first << ", " << lower_right.second << " = " << x << ", " << y << std::endl;

// 0 -65.656000, 24.093800 = -7308792.487523, 2747405.191359  --> These are correct

char *pszDstWKT = NULL;

char *pszSrcWKT = NULL;

OSRExportToWkt( mercator_proj, &pszDstWKT );

OSRExportToWkt( wgs84_proj, &pszSrcWKT );

void *hTransformArg;

hTransformArg = GDALCreateGenImgProjTransformer( NULL, pszSrcWKT, NULL, pszDstWKT, FALSE, 0, 1 );

x = upper_right.first;

y = upper_right.second;

int success;

result = GDALGenImgProjTransform(hTransformArg, FALSE, 1, &x, &y,  NULL, &success);

std::cout << result << " " << upper_right.first << ", " << upper_right.second << " = " << x << ", " << y << std::endl;

// 1 -65.656000, 49.718600 = -7308792.487523, 6365068.664114 --> This is now correct as well

x = lower_right.first;

y = lower_right.second;

result = GDALGenImgProjTransform(hTransformArg, FALSE, 1, &x, &y, NULL, &success);

std::cout << result << " " << lower_right.first << ", " << lower_right.second << " = " << x << ", " << y << std::endl;

//  1 -65.656000, 24.093800 = -7308792.487523, 2747405.191359 --> These are correct


From: [hidden email] on behalf of Adam Nowacki
Sent: Mon 11/2/2009 4:49 PM
To: [hidden email] >> Gdal-Dev
Subject: Re: [gdal-dev] Building a Mercator Image from Un-projected data..Major confusion and help requested.

Cassanova, Bill wrote:


> result = GDALGenImgProjTransform(hTransformArg, TRUE, 1, &x, &y,
>                               NULL, &success);
> std::cout << std::fixed;
> std::cout << result << " " << lower_right.first << ", " <<
> lower_right.second << " = " << x << ", " << y << std::endl;
>
> *//** 1 -65.656000, 49.718600 = -0.000590, 0.000450***
>
> *// This is NOT Right** so apparently I am doing something wrong or not
> understanding the concept here*

2nd argument to GDALGenImgProjTransform is TRUE so you are reprojecting
from, not to mercator
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev


_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Christopher Barker

Re: Building a Mercator Image from Un-projected data..Major confusion and help requested.

Reply Threaded More More options
Print post
Permalink
Cassanova, Bill wrote:
> My question is how does this convert to pixel location FROM
> the data set and then INTO the image.

Each pixel center is at a given lat-long in each image, but with
different projections, these are different. So you need to do:

pixel1 -> lat-long

lat-long -> meters_in_new_projection

meters_in_new_projection -> pixel2

This is going to take a bit of arithmetic.

However, a pixel in one image is not going to map exactly to one pixel
in the other -- so you need to do some type of interpolation. nearest
neighbor is easy, but not very pretty. Other methods are more complicated.

gdalwarp does all this for you, and does it well, so I'd either call out
to the command line program (easiest), or look at the gdalwarp source
code, and see if you can wrap it up in your app. No need to start from
scratch.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev
Christopher Barker

Re: Building a Mercator Image from Un-projected data..Majorconfusion and help requested.

Reply Threaded More More options
Print post
Permalink
Cassanova, Bill wrote:
> Thank you for your response.

> Your email motivated me to try something.  Not sure it's quite right but
> it does seem closer:

It looks right at a quick glance, but note that you are essentially
re-writing gdalwarp -- is that code too opaque to make use of?

> I ran the gdalwarp process on an unprojected image and as far as I can
> tell the image produced by this code and the image produced from that
> code are exactly the same.

right -- by default, gdalwarp uses nearest neighbor interpolation, which
is pretty much what you've done. You might try one of the other
interpolation methods, and see if the results look nicer:

$ gdalwarp
Usage: gdalwarp [--help-general] [--formats]
     [-s_srs srs_def] [-t_srs srs_def] [-to "NAME=VALUE"]
     [-order n] [-tps] [-rpc] [-geoloc] [-et err_threshold]
     [-te xmin ymin xmax ymax] [-tr xres yres] [-ts width height]
     [-wo "NAME=VALUE"] [-ot Byte/Int16/...] [-wt Byte/Int16]
     [-srcnodata "value [value...]"] [-dstnodata "value [value...]"]
-dstalpha
     [-r resampling_method] [-wm memory_in_mb] [-multi] [-q]
     [-cutline datasource] [-cl layer] [-cwhere expression]
     [-csql statement] [-cblend dist_in_pixels]
     [-of format] [-co "NAME=VALUE"]*
     srcfile* dstfile

Available resampling methods:
     near (default), bilinear, cubic, cubicspline, lanczos.


so you should be abel to do (untested):

gdalwarp -s_srs '+proj=merc' -r cubic
hiradff_200910121800_f180_weather_wx.tiff
warped.tiff

It depends some on your image, but it could look quite a bit nicer.

-Chris


--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
gdal-dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/gdal-dev