Rasterlite2 function like rasterliteGetRasterByRect

144 views
Skip to first unread message

Majid Hojati

unread,
Aug 25, 2017, 4:21:43 PM8/25/17
to SpatiaLite Users
Hi,
Is there any functions such as rasterliteGetRasterByRect which works in rasterlite for rasterlite2?
I looked at this link https://www.gaia-gis.it/fossil/librasterlite2/wiki?name=sql_reference_list but could not find any similar functions.
Thanks very much

Brad Hards

unread,
Aug 25, 2017, 11:26:48 PM8/25/17
to spatiali...@googlegroups.com
Are you just looking for RL2_GetMapImageFromRaster() ?
If not, can you explain more about what you need?

Brad


Majid Hojati

unread,
Aug 26, 2017, 12:43:55 PM8/26/17
to SpatiaLite Users
Hi,
To be honest I am new with rasterlite2, and I want to use its c++ api. I have seen rasterliteGetRasterByRect function which returns raster based on rectangular but I dont know if there is any relevant function in rasterlite2. In fact there is not any api documents for rasterlite2 which I can use them, How can I find a api relevant to  RL2_GetMapImageFromRaster() in C++?

mj10777

unread,
Aug 26, 2017, 1:04:15 PM8/26/17
to SpatiaLite Users


On Saturday, 26 August 2017 18:43:55 UTC+2, Majid Hojati wrote:
Hi,
To be honest I am new with rasterlite2, and I want to use its c++ api. I have seen rasterliteGetRasterByRect function which returns raster based on rectangular but I dont know if there is any relevant function in rasterlite2. In fact there is not any api documents for rasterlite2 which I can use them, How can I find a api relevant to  RL2_GetMapImageFromRaster() in C++?
The intention is to use Sql-Queries only to retrieve results.

SELECT
RL2_GetMapImageFromRaster
(
 -- coverage_name
 '1890.berlin_postgrenzen',
 -- Rectangle to extract
 BuildMBR(20800.0,22000.0,24000.0,19600.0),
 -- Image size[together with Rectangle = Resolution]
 1200,1920,
 -- style to use (if any)
 'default',
 -- Image format to return result
 'image/png',
 -- background color
 '#ffffff',
 0,0,1);

https://www.gaia-gis.it/fossil/librasterlite2/wiki?name=sql_reference_list

look for 'SQL functions returning styled Maps'.

In c++ you would execute the Sql and retrieve the blob data and create an image (in this case a png) from that data.

Hope this helps

Mark

Majid Hojati

unread,
Aug 27, 2017, 1:05:16 AM8/27/17
to SpatiaLite Users
Hi,
Well You mean I must just query the results and convert them into png for example. The hard part is its conversion from BloB to png. 

mj10777

unread,
Aug 27, 2017, 1:41:09 AM8/27/17
to SpatiaLite Users


On Sunday, 27 August 2017 07:05:16 UTC+2, Majid Hojati wrote:
Hi,
Well You mean I must just query the results and convert them into png for example. The hard part is its conversion from BloB to png. 
Not really, most of these function have a constructor to create from a ByteArray. 
-- QT:
QByteArray imageData = sqlite3_column_blob(stmt,0);
QImage result_image=QImage::fromData( imageData );

-- wxWindows:
const unsigned char *p_blob = (const unsigned char *) sqlite3_column_blob(stmt, 0);
wxImage result_image = wxImage(FrameWidth, FrameHeight);
result_image.SetData(p_blob, false);

 

Just remember, that what Image-Type is set in the RL2_GetMapImageFromRaster command (my sample PNG)
- so that the returned BLOB (BytArray) is already in that format.

Brad Hards

unread,
Aug 27, 2017, 2:05:33 AM8/27/17
to spatiali...@googlegroups.com
> Well You mean I must just query the results and convert them into png for
> example. The hard part is its conversion from BloB to png.
I don't understand the problem. The blob _is_ a PNG image, in the same way
that C++ would have given you an object or byte array or similar that holds
the PNG data.

Brad


Majid Hojati

unread,
Aug 28, 2017, 10:51:45 AM8/28/17
to SpatiaLite Users
Hi,
Thanks very much. If I want to use Rasterlite2 api is there any procedures for it? I have to then convert images to mapnik::image so I must get RGBA data from rasterlite2.

For example in this code I am going to read part of image to a rasterptr and get its extends but it does not work

rl2CoveragePtr coverage= rl2_create_coverage_from_dbms(db_,cov);
        const char *a2= rl2_get_coverage_name(coverage);
        rl2_get_coverage_resolution(coverage,&hres,&vres);

//        rl2_get_raw_raster_data(db_,20,coverage,200,300,49.47,34.472,54.147,37.687,72.0,72.0,buffer,
//                                buffer_s,pallet,RL2_PIXEL_GRAYSCALE);
    //    rl2_export_raw_pixels_from_dbms(db_,20,coverage,72,72,49.47,34.472,54.147,37.687,300,300,1,&buffer,
     //                                   &buffer_s);
          double minx=0,miny=0,maxx=0,maxy=0;
          double ex_minx=49.47,ex_miny=34.47,ex_maxx=54.147,ex_maxy=37.67;
          int hight=(int)((ex_maxy-ex_miny)/hres);
          int width=(int)((ex_maxx-ex_minx)/vres);
        if (rl2_get_raw_raster_data
            (db_, 20, coverage, width, hight,49.47,34.472,54.147,37.687, hres, vres,&buffer,
             &buffer_s, NULL,RL2_PIXEL_RGB) != RL2_OK){
            rl2RasterPtr rst= rl2_create_raster (width,hight, RL2_SAMPLE_UINT8, RL2_PIXEL_RGB, 3.0,
                       buffer, buffer_s, NULL, NULL, 0.0, NULL);
            rl2_export_jpeg_from_dbms(db_, 20,"test.jpg",
                        coverage, hres, vres,49.47,34.472,54.147,37.687,width, hight,72.0,1);
            rl2_get_raster_extent(rst,&minx,&miny,&maxx,&maxy);
        }
 
The above code returns 0 in rl2_get_raw_raster_data part which I think I am doing somthing wrong. It would be great if we had some documentations or examples.

Majid Hojati

unread,
Aug 28, 2017, 10:53:01 AM8/28/17
to SpatiaLite Users
What if we want to get images directly using  C++ api?

mj10777

unread,
Aug 28, 2017, 11:03:17 AM8/28/17
to SpatiaLite Users


On Monday, 28 August 2017 16:51:45 UTC+2, Majid Hojati wrote:
Hi,
Thanks very much. If I want to use Rasterlite2 api is there any procedures for it? I have to then convert images to mapnik::image

mj10777

unread,
Aug 28, 2017, 11:13:58 AM8/28/17
to SpatiaLite Users


On Monday, 28 August 2017 16:53:01 UTC+2, Majid Hojati wrote:
What if we want to get images directly using  C++ api?
That would be your problem.
You are just making your life more difficult. 

Mark

a.fu...@lqt.it

unread,
Aug 28, 2017, 11:33:22 AM8/28/17
to spatiali...@googlegroups.com
On Mon, 28 Aug 2017 07:53:01 -0700 (PDT), Majid Hojati wrote:
> What if we want to get images directly using  C++ api?
>

Hi Majid,

note: there is no such thing as a C++ api.
Rasterlite2 is fully written in pure C, without any
plusplus frill.
you could eventually check the following two API:

rl2RasterPtr rl2_raster_from_png (const unsigned char *png,
int png_size, int alpha_mask);

int rl2_raster_data_to_RGBA (rl2RasterPtr rst,
unsigned char **buffer, int *buf_size);

please read the Doxygen documentations or the source
code (headers/rasterlite2/rasterlite2.h) for more
details.

bye Sandro

Majid Hojati

unread,
Aug 28, 2017, 3:00:22 PM8/28/17
to SpatiaLite Users
wow, thanks I think everything is easy now. 

Majid Hojati

unread,
Aug 28, 2017, 3:01:31 PM8/28/17
to SpatiaLite Users
So you mean It is better to use sql functions than using api right?I was trying to use APIs biut sql functions are much easier to use

Majid Hojati

unread,
Aug 28, 2017, 3:02:57 PM8/28/17
to SpatiaLite Users
well thanks very much, Can you please share ma a link for Doxygen documentations? I could not find its link on rasterlite's website.

a.fu...@lqt.it

unread,
Aug 28, 2017, 3:47:55 PM8/28/17
to spatiali...@googlegroups.com
On Mon, 28 Aug 2017 12:02:57 -0700 (PDT), Majid Hojati wrote:
> well thanks very much, Can you please share ma a link for Doxygen
> documentations? I could not find its link on rasterlite's website.
>

Sorry, I was overoptimistic.

The Doxygen doc isn't yet available; as a replacement please read
the comments into the header files (*.h)

bye Sandro

Majid Hojati

unread,
Aug 28, 2017, 3:56:36 PM8/28/17
to SpatiaLite Users
thanks very much dear Sandro,

Majid Hojati

unread,
Aug 29, 2017, 6:05:03 AM8/29/17
to SpatiaLite Users
Hi 
can you give me a hint about the use of 
 -- Rectangle to extract
 BuildMBR(20800.0,22000.0,24000.0,19600.0),
 -- Image size[together with Rectangle = Resolution]
 1200,1920,

My first question is about MBR. Does the coordinates have to be based on SRID of input data? for example here MBR is coordinates based on UTM?
Also what about image size? does it must be based on resolution? I mean I have to calculate it based on this equation? (minx-maxx/resolution)=mage width?

mj10777

unread,
Aug 29, 2017, 6:23:06 AM8/29/17
to SpatiaLite Users


On Tuesday, 29 August 2017 12:05:03 UTC+2, Majid Hojati wrote:
Hi 
can you give me a hint about the use of 
 -- Rectangle to extract
 BuildMBR(20800.0,22000.0,24000.0,19600.0),
 -- Image size[together with Rectangle = Resolution]
 1200,1920,

My first question is about MBR. Does the coordinates have to be based on SRID of input data? for example here MBR is coordinates based on UTM?
Yes. 
Also what about image size? does it must be based on resolution? I mean I have to calculate it based on this equation? (minx-maxx/resolution)=mage width?
Depends on which function you are using:
RL2_GetMapImageFromRaster
- you must give the extent and the image size
--> it will calculate the resolution needed for that

If  you want an Image based in a specific resolution
- you must calculate the image with and height:

SELECT 'Adlerstraßen' AS name,
 RL2_GetMapImageFromRaster
 (
  -- The dbPrefix argument is intended to specify the ATTACHED-DB where the Coverage is expected to be found
  'main',
  -- chosen name of raster_coverage
  lower('1910.alt_berlin_coelln_friedrichsweder'),
  -- area to extract,
  BuildMbr
  (
   24500.0,21030.0, 24900.0,20630.0,
   3068
  ),
  -- image-size width,height (calculate)
 -- 1181
 CAST(round((24900.0-24500.0)/0.338695578413350) AS INTEGER),
 -- 1181
 CAST(round((21030.0-20630.0)/0.338695578413350) AS INTEGER),
  -- style-name
  'default',
  -- image/mime-type jpeg
  'image/png',
  -- background
  '#ffffff',
  -- transparent
  0,
  -- jpeg quality
  80,
  -- 1 = adapt image width,height if needed
  1
 ) AS image;

The function must receive the coordinates based on the SRID of the raster_coverage.
- so you then must add a around the BuildMbr, so that the result is in the srid of the raster_coverage:
--> ST_Transform(BuildMbr(10,20,30,40,4326),3068)

a.fu...@lqt.it

unread,
Aug 29, 2017, 6:40:12 AM8/29/17
to spatiali...@googlegroups.com
On Tue, 29 Aug 2017 03:05:03 -0700 (PDT), Majid Hojati wrote:
> Hi 
> can you give me a hint about the use of 
>
>>  -- Rectangle to extract
>>  BuildMBR(20800.0,22000.0,24000.0,19600.0),
>>  -- Image size[together with Rectangle = Resolution]
>>  1200,1920,
>
> My first question is about MBR. Does the coordinates have to be based
> on SRID of input data? for example here MBR is coordinates based on
> UTM?
>

hi Majid,

the MBR is intended to define the Map Extent covered by the
returned image: it's normally expected to be expressed in the
"natural" SRID of the Coverage.
if not, an appropriate "reprojection on the fly" will be computed
if the requested SRID is one supported by the Coverage.
please see the SpatiaLite's SQL functions:
SE_RegisterRasterCoverageSRID()
and
SE_RegisterVectorCoveragSRID()

NOTE: at the current state of development "reprojection on
the fly" is only implemented for Vector Coverages, but not
yet for Raster Coverages.


> Also what about image size? does it must be based on resolution? I
> mean I have to calculate it based on this equation?
> (minx-maxx/resolution)=mage width?
>

RasterLite2 has the intrinsic capability to automatically
identify the most convenient Pyramid resolution, then
rescaling the output image as appropriate.

The relevant thing is that the width/height ratio _MUST_
be exactly the same measured in map units _AND_ in pixels.

Just a trivial example:
- assuming a worldwide map (SRID=4326, long/lat),
the extreme points of this map (diagonal) will be
(-180,-90) and (180,90), leading to a width/height
ratio = 2.0
- the width and height measured in pixels _MUST_
respect a 2.0 ratio, so valid values could be:
* 1024 x 512
* 2048 x 1024
* 550 x 275
* 2468 x 1234
* ... and so on ...

The Map Scale of the output image will consequently
depend on the ratio between pixels and map units:
1px = X map units

bye Sandroo

Majid Hojati

unread,
Aug 29, 2017, 10:34:08 AM8/29/17
to SpatiaLite Users
wow, thanks for your help, this was exactly where I was faced a problem. It is fixed now,
The only thing which I think is very useful while I am working on mapniks input plugin is band composition, I looked at examples of rasterlite2 and find out it is possible to composite bands using styles such as

<ChannelSelection>
<RedChannel>
<SourceChannelName>3</SourceChannelName>
</RedChannel>
<GreenChannel>
<SourceChannelName>2</SourceChannelName>
</GreenChannel>
<BlueChannel>
<SourceChannelName>1</SourceChannelName>
</BlueChannel>
</ChannelSelection>
<ContrastEnhancement>
<Histogram/>
</ContrastEnhancement>

but could not find any sql functions to composite colors. is it possible to use rt2 functions?or I must use dynamic styles to change number of bands in sld file?

Majid Hojati

unread,
Aug 29, 2017, 10:39:20 AM8/29/17
to SpatiaLite Users
Thanks very much for your information, now I can easily calculate image width and hight from mapniks query parameters.
But I have faced a problem with this function

 int img_size=0;
        int state=0;
        const char * img_buff = sqlite_utils::get_png(dataset_,cov_info,40,38,55,45,1200,600,state,img_size);
       // int img_size = strlen(img_buff);
        if(state==0) {
            rl2RasterPtr rst = rl2_raster_from_png (img_buff,img_size,0);

            unsigned char* RGBA_buff;
            int RGBA_size;
            int state_rgba = rl2_raster_data_to_RGBA (rst,&RGBA_buff, &RGBA_size);
        } else {
            cout<<"\r\nERROR";
        }


In my function sqlite_utils::get_png works fine and returns blob form database but while I use   rl2RasterPtr rst = rl2_raster_from_png (img_buff,img_size,0);  It returns rst=0 which Is somehow wrong. Should I check anything in databases metadata table?

a.fu...@lqt.it

unread,
Aug 29, 2017, 12:52:27 PM8/29/17
to spatiali...@googlegroups.com
On Tue, 29 Aug 2017 07:34:08 -0700 (PDT), Majid Hojati wrote:
> The only thing which I think is very useful while I am working on
> mapniks input plugin is band composition, I looked at examples of
> rasterlite2 and find out it is possible to composite bands using
> styles such as
>
> -------- <snip> SE RasterSymbolizer example <snip> --------------
>
> but could not find any sql functions to composite colors. is it
> possible to use rt2 functions?or I must use dynamic styles to change
> number of bands in sld file?
>

Majid,

if you notice both RL2_GetMapImageFromRaster and
RL2_GetMapImageFromVector accept a "styleName"
argument.
you are just required to pass to these SQL functions
the name of some appropriate Style you wish to
be applied to your rendered Map Image.

explained in very few words, the general workflow for
defining and referencing SLD/SE styles in Rasterlite2
is as follows:

1. write some XML file defining the style
2. load and parse the XML file into the DB using
SE_RegisterRasterStyle (or SE_RegisterVectorStyle in
the case of VectorSymbolizers).
3. associate the Style to the Coverage using
SE_RegisterRasterStyledLayer (or respectively
SE_RegisterVectorStyledLayer)
4. after all these operations you'll then be able
to directly reference the Style by its name when
calling RL2_GepMapImageFromRaster or
RL2_GetMapImageFromVector.

Note: you can eventually write by hand your XML styles
using any text editor.
I warmly recommend you to use the user friendly
tools and wizards available on spatialite_gui
(2.1-devel); you'll probably discover they can
make your life a lot much simpler and easier.

bye Sandro


Majid Hojati

unread,
Aug 29, 2017, 1:24:57 PM8/29/17
to SpatiaLite Users
Hi,
Thanks for your answer, so you mean there is not any way to do it by code?For example every time I want to load a different color composition I have to save that style to a file and then add it into database and then use it, If there would be some functions to load style into database using an string it would be great, So I just could populate style in my application and then add it into database. Or this could be done using rl2 api functions?

a.fu...@lqt.it

unread,
Aug 29, 2017, 2:37:33 PM8/29/17
to spatiali...@googlegroups.com
On Tue, 29 Aug 2017 10:24:56 -0700 (PDT), Majid Hojati wrote:
> Hi,
> Thanks for your answer, so you mean there is not any way to do it by
> code?For example every time I want to load a different color
> composition I have to save that style to a file and then add it into
> database and then use it,
>

Majid,

once you've inserted a style into the DB it becomes a
persistent object.
so you just have to prepare in advance your DB (including
all the styles you intend to support) and that's all.


> If there would be some functions to load
> style into database using an string it would be great, So I just
> could
> populate style in my application and then add it into database. Or
> this could be done using rl2 api functions?
>

there are many different ways to load Styles into the DB.
please read the documentation:

https://www.gaia-gis.it/fossil/libspatialite/wiki?name=SLD/SE+Styling

and also check the latest HTML doc about spatialite's SQL
functions (look at the "SQL functions supporting SLD/SE
Styled Layers" section).
you'll find an updated copy of "spatialite-sql-latest.html"
within the sources you can download from the Fossil repository.

bye Sandro

a.fu...@lqt.it

unread,
Aug 29, 2017, 5:03:11 PM8/29/17
to spatiali...@googlegroups.com
On Tue, 29 Aug 2017 07:39:20 -0700 (PDT), Majid Hojati wrote:
> But I have faced a problem with this function
>
>  int img_size=0;
>  int state=0;
>  const char * img_buff =
>
> sqlite_utils::get_png(dataset_,cov_info,40,38,55,45,1200,600,state,img_size);
>  // int img_size = strlen(img_buff);
>

Majid,

there are some obvious errors in the above code snippet.

1. "img_size" is expected to represent the size (in bytes)
of the PNG BLOB; it must be set to the appropriate value
by your "sqlite_utiles::get_png()" function, so it
requires to be referenced by a pointer.
something like: get_png(...., &img_size);
in your example "img_size" will always be ZERO, and
any attempt do decode an empty BLOB will obviously
fail.

2. you correctly commented the strlen() line; a BLOB isn't
a null delimited string, and you must never call strlen()
on behalf of a BLOB, or nasty errors (and may be a fatal
crash) will surely follow.

3. minor, but still relevant: a BLOB contains binary data,
so "img_buff" should be declared as "unsigned char *img_buff".


very sketchy summary about properly handling BLOB values
returned by a SQL prepared statement (sqlite3_stmt *):

unsigned char *blob = NULL;
int ret = sqlite3_step(stmt);
if (ret == SQLITE_ROW)
{
if (sqlite3_column_type(stmt, 0) == SQLITE_BLOB)
{
const unsigned char *ptr =
sqlite3_column_blob(stmt, 0);
int sz = sqlite3_column_bytes(stmt, 0);
blob = (unsigned char *)malloc(sz);
memcpy(blob, ptr, sz);
*img_size = sz;
}
}
return blob;

- resultset values are transient and can unexpectedly
change; always saving BLOBs into dynamic memory
(malloc/memcpy) is a good programming practice that
will robustly shield your code.
- there is only an unique way to determine the size
of a BLOB measured in bytes, and it's by calling
sqlite_column_bytes(); this value must be carefully
stored somewhere and passed back to the caller.

bye Sandro

Majid Hojati

unread,
Aug 30, 2017, 12:49:01 PM8/30/17
to SpatiaLite Users
Dear Sandro ,
Thanks very much for the links and your help, I'll read those function.

Majid Hojati

unread,
Aug 30, 2017, 12:52:27 PM8/30/17
to SpatiaLite Users
Dear Sandro,
Thank you for your great explanations about code, I am new to c++ and I hope I do not make such mistakes in future. I'll use your suggestions and let you know the results.
Thanks very much.
Majid

Majid Hojati

unread,
Sep 26, 2017, 1:52:11 AM9/26/17
to SpatiaLite Users
Dear Sandro,
Hi,
I have faced an strange problem with UTM coordinate system, To calculate image size I have used the following code:


   double x0, y0, x1, y1;//This is defined to get coverage extents in 4326
    x0=coverage.geo_minx;
    y0=coverage.geo_miny;
    x1=coverage.geo_maxx;
    y1=coverage.geo_maxy;
    box2d<double> raster_extent(x0, y0, x1, y1);
    box2d<double> intersect = raster_extent.intersect(q.get_bbox());//we have a query object in mapnik which requests a bbox and also has a resolution.

    const int width = static_cast<int>(std::get<0>(q.resolution()) * intersect.width() + 0.5);
    const int height = static_cast<int>(std::get<0>(q.resolution()) * intersect.height() + 0.5);
    const double pixel_size = (intersect.width() >= intersect.height()) ?
            (intersect.width() / (double) width) : (intersect.height() / (double) height);

My Log is shown here:
Mapnik LOG> 2017-09-25 14:23:57: rasterlite_featureset: Coverage extent=box2d(44.9995836965389984,29.9990570224555348,55.0011049965362488,40.0004167635142949)
Mapnik LOG> 2017-09-25 14:23:57: rasterlite_featureset: View extent=box2d(44.9995836965389984,29.9990570224555348,55.0011049965362488,40.0004167635142949)
Mapnik LOG> 2017-09-25 14:23:57: rasterlite_featureset: Intersect extent=box2d(44.9995836965389984,29.9990570224555348,55.0011049965362488,40.0004167635142949)
Mapnik LOG> 2017-09-25 14:23:57: rasterlite_featureset: Query resolution=59.9918,59.9918
Mapnik LOG> 2017-09-25 14:23:57: rasterlite_featureset: Size=600 600
Mapnik LOG> 2017-09-25 14:23:57: rasterlite_featureset: Pixel Size=0.0166692
Mapnik LOG> 2017-09-25 14:23:57:  get_image sql :  SELECT RL2_GetMapImageFromRaster ('srtmdem',ST_Transform(BuildMbr(44.9996,29.9991,55.0011,40.0004,4326),3857),600,600,'srtm_plus','image/png','#ffffff',1,80,1) as image

But it returns null, I think the image size is not valid. But I have no idea where am I doing wrong. Can you please give me some hints about my possible mistakes?

a.fu...@lqt.it

unread,
Sep 26, 2017, 2:16:57 PM9/26/17
to spatiali...@googlegroups.com
On Mon, 25 Sep 2017 22:52:11 -0700 (PDT), Majid Hojati wrote:
> SELECT RL2_GetMapImageFromRaster ('srtmdem',
> ST_Transform(BuildMbr(44.9996,29.9991,55.0011,40.0004,4326),3857),
> 600,600,'srtm_plus','image/png','#ffffff',1,80,1) as image
>
> But it returns null, I think the image size is not valid. But I have
> no idea where am I doing wrong. Can you please give me some hints
> about my possible mistakes?
>

Hi Majid,

it's very hard replying to a question like this, because too many
relevant factors are not specified. I'll attempt to guess at blind
starting from the most plausible causes:

a. you are passing a BBOX defined in the SRID=3857; is this one
the native SRID of the 'srtmdem' Coverage ?
I strongly doubt, because the SRTM dataset is usually based
on WGS84 (SRID=4326).

b. if 3857 isn't the native SRID, you are implicitly requesting
for a raster reprojection. but in this case RasterLite2
expects to be explicitly authorized (as defined in the config
metatables) to apply such a transformation.
just test the following SQL query:
SELECT * FROM raster_coverages_ref_sys
WHERE coverage_name = 'srtmdem' AND srid = 3857;
if it return an empty resultset transforming from 4326 to 3857
will be always forbidden (yet another inheritance from WMS).
you necessarily have to call ST_RegisterRasterCoverageSrid()
in order to explicitly authorize any raster reprojection.

c. and finally the most evident issue; your initial BBOX expressed
in SRID=4326 is a square.
but you can _NEVER_ assume that it will still be a square
after applying a reprojection into another SRID, because it
will easily become a rectangle or a trapezoid, thus dramatically
changing the width / height ratio of the returned raster.

see the attached figure exemplifying the following transformations:

A) SELECT BuildMbr(44.9996,29.9991,55.0011,40.0004,4326);
B) SELECT
ST_Transform(BuildMbr(44.9996,29.9991,55.0011,40.0004,4326),3857);
C) SELECT
ST_Transform(BuildMbr(44.9996,29.9991,55.0011,40.0004,4326),32638);

bye Sandro
bboxes.png

Majid Hojati

unread,
Sep 26, 2017, 2:24:02 PM9/26/17
to SpatiaLite Users
Dear sandro,
Hi,
Thanks for your answer, About the first item, I've reprojected srtm dems into SRID 3857, this is why I am reprojecting my rectangular, My request from mapnik comes from 4326 SRID so I have to reproject the request area, What other options do you suggest?
Also what about calculations of width and hight? Are they correct?

a.fu...@lqt.it

unread,
Sep 27, 2017, 3:44:03 AM9/27/17
to spatiali...@googlegroups.com
On Tue, 26 Sep 2017 11:24:01 -0700 (PDT), Majid Hojati wrote:
> Dear sandro,
> Hi,
> Thanks for your answer, About the first item, I've reprojected srtm
> dems into SRID 3857, this is why I am reprojecting my rectangular, My
> request from mapnik comes from 4326 SRID so I have to reproject the
> request area,
>

Majid,

I'm sorry, but it seems a messy nonsense.

- the SRTM dataset is natively based on SRID=4326, but you've
reprojected it on SRID=3857 (how ? may be using gdalwarp ?)
- then you are asking RasterLite2 to reproject back again to
SRID=4326
- if your specific goal is the one to display on the screen
poor quality raster maps imposing at the same time a very
high CPU workload you are pointing in the right direction


> What other options do you suggest ?
>

just create a Raster Coverage directly based on SRID=4326
for your SRTM data, so to avoid any unneeded reprojection.


> Also what about calculations of width and hight? Are they correct?
>

the general workflow usually adopted by many applications
works the opposite way:
- both width and height are fixed once for all, so to nicely
preserve in a fully consistent way the same image size
for subsequent "GetMap" requests.
- for "pan requests" you'll simply have to appropriately
translate the image's BBOX leaving the current resolution
unchanged.
- for "zoom requests" you'll change first the current
resolution, then recomputing the image's BBOX.
a commonly adopted solution is to maintain untouched
the center point position, then appropriately recomputing
the positions of the four corners.

in any case you are never expected to directly face any
BBOX related question when applying a reprojection
between two different SRIDs; just pass the appropriate
image BBOX (in the output "map" SRID), and RasterLite2
will take care to automatically adapt such BBOX to the
native BBOX of the underlaying Coverage.

bye Sandro
Message has been deleted
Message has been deleted
Message has been deleted

Majid Hojati

unread,
Sep 27, 2017, 3:42:11 PM9/27/17
to SpatiaLite Users
Hi,
I'm sorry, but it seems a messy nonsense. 

- the SRTM dataset is natively based on SRID=4326, but you've 
   reprojected it on SRID=3857 (how ? may be using gdalwarp ?) 
- then you are asking RasterLite2 to reproject back again to 
   SRID=4326 
- if your specific goal is the one to display on the screen 
   poor quality raster maps imposing at the same time a very 
   high CPU workload you are pointing in the right direction 

No, In fact I have reprojected my srtm data into 3857 using gdalwarp then I have added them into a coverage with 3857 and  then as mapnik always requests tiles in 4326 I have to create an Mbr in 4326 and reproject it into 3857 to be able to request an area. Am I doing wrong?

in any case you are never expected to directly face any 
BBOX related question when applying a reprojection 
between two different SRIDs; just pass the appropriate 
image BBOX (in the output "map" SRID), and RasterLite2 
will take care to automatically adapt such BBOX to the 
native BBOX of the underlaying Coverage. 
As I must insert data into a coverage with srid=3875 I must query a bbox with 3875 and as mapnik passes a request only in 4326 I am forced to reproject Mbr in my queries, Am I doing wrong?

I have checked different versions of rl2 and find out that the latest version has some kind of different bahaviours than others.
I have a database with srtm coverage (4326),
This query works fine with this version :librasterlite2-1.0.0-devel
  SELECT RL2_GetMapImageFromRaster ('srtmdem',ST_Transform(BuildMbr(44.9996,29.9996,55.0004,40.0004,4326),4326),600,600,'srtm_plus','image/png','#ffffff',1,80,1) as image

but with this one :librasterlite2-c19226c1a6
It returns Null, I have tried different Mbrs and this is true for all of them,
well wmslite with librasterlite2-1.0.0-devel recognizes database as a valid database but this one :librasterlite2-c19226c1a6 returns an error,
Gdal driver works with librasterlite2-c19226c1a6 but with librasterlite2-1.0.0-devel it does not work.

I think sth is changed which I am not noticed. 
I also tried to change coverages SRID to 3857, I reprojected images and then imported into another database, none of my queries worked as I have to use ST_Transform(BuildMbr(44.9996,29.9996,55.0004,40.0004,4326),3857) to create mbr and it seems I am miscalculating width and hight
Thanks very much 
Majid 

Majid Hojati

unread,
Sep 28, 2017, 2:32:15 AM9/28/17
to SpatiaLite Users
I looked at the source code and find out that rl2 automatically reprojects the requested area into coverages SRID so I only need to pass an Mbr in 4326 and rl2 handles every thing. the only problem is with calculation of width and hight which I have faced problem so far

a.fu...@lqt.it

unread,
Sep 28, 2017, 5:08:28 AM9/28/17
to spatiali...@googlegroups.com
On Wed, 27 Sep 2017 23:32:14 -0700 (PDT), Majid Hojati wrote:
> I looked at the source code and find out that rl2 automatically
> reprojects the requested area into coverages SRID so I only need to
> pass an Mbr in 4326 and rl2 handles every thing.
>

Hi Majid,

you've finally got the point.
it's exactly what I was attempting to explain you in my last replies.


> the only problem is
> with calculation of width and hight which I have faced problem so far
>

it usually works in the opposite way.
you are usually expected to handle pre-defined width and height
(e.g. because these are the dimensions of the screen window
requiring to be updated/repainted).
so you basically have just to recalculate the positions of the
four corners of your BBOX accordingly to the current scale and
centre point position (a very trivial task).

bye Sandro

Majid Hojati

unread,
Sep 28, 2017, 8:39:30 AM9/28/17
to SpatiaLite Users
Dear Sandro,
Thanks very much for your help, But when I query a Mbr for a coverage with 3857 SRID it gives me null, But when I query the same extent in 4326 with the same images but in 4326 SRID it returns in image.

Could you please check this part from my last reply:

I have checked different versions of rl2 and find out that the latest version has some kind of different bahaviours than others.
I have a database with srtm coverage (4326),
This query works fine with this version :librasterlite2-1.0.0-devel
  SELECT RL2_GetMapImageFromRaster ('srtmdem',ST_Transform(BuildMbr(44.9996,29.9996,55.0004,40.0004,4326),4326),600,600,'srtm_plus','image/png','#ffffff',1,80,1) as image

but with this one :librasterlite2-c19226c1a6
It returns Null, I have tried different Mbrs and this is true for all of them,
well wmslite with librasterlite2-1.0.0-devel recognizes database as a valid database but this one :librasterlite2-c19226c1a6 returns an error,
Gdal driver works with librasterlite2-c19226c1a6 but with librasterlite2-1.0.0-devel it does not work.

I think sth is changed which I am not noticed. 
I also tried to change coverages SRID to 3857, I reprojected images and then imported into another database, none of my queries worked as I have to use ST_Transform(BuildMbr(44.9996,29.9996,55.0004,40.0004,4326),3857) to create mbr and it seems I am miscalculating width and hight
Thanks very much 
Majid 

It seems the newer versions of rl2 has some changes which leads to a null result for the same database and the same query. 

Majid Hojati

unread,
Oct 1, 2017, 10:34:51 AM10/1/17
to SpatiaLite Users

Dear Sandro,

I tried to load rl2 coverage which was in 3857 using wmslite on qgis, there was an intresting result.
here it is images in two zoom levels


well at first I thought there is something wrong with my reprojected images, so I opened them in qgis




As screenshot shows images are fine, So it seems there is sth wrong with multyresulution coverages, As I noticed the only difference beside different SRIDs is that in coverage with 3875 we must set resolution parameter to mixed but in 4336 this is not necessary,
Sorry for my continues questions, I just like to learn and if possible contribute at least by testing and so on,
Thanks 
Majid 

a.fu...@lqt.it

unread,
Oct 2, 2017, 6:10:53 AM10/2/17
to spatiali...@googlegroups.com
On Sun, 1 Oct 2017 07:34:50 -0700 (PDT), Majid Hojati wrote:
> Sorry for my continues questions, I just like to learn and if
> possible
> contribute at least by testing and so on,
>

Majid,

it seems to me that you are now pointing on the right direction;
directly testing WMS coverages on QGIS surely is a good method
so to really understand how RL2 do actually works.

there is no "magic spell" helping a novice developer to find
its own way in an unexplored world; use your own common sense
and learn by your previous mistakes by trial and error.
in other worlds: never depend on the "authority" or "revealed
truth" coming from outside; just depend on your own mind and
personal direct experience ... this always is the most effective
way for learning ;-)

bye,
Sandro



Majid Hojati

unread,
Oct 2, 2017, 3:52:37 PM10/2/17
to SpatiaLite Users
Dear Sandro,
Hi,
Thanks for your helps and patient in answering questions. 
Reply all
Reply to author
Forward
0 new messages