Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is it possible to query a PostGIS database from a browser where I set the extents of the map?

I have previously queried a PostGIS database from the browser and, separately, used mapserver to display a raster using a WMS query. Now I'm wondering if I can join these two separate pieces together. The part that I am not understanding is how to insert the query into the Mapserver code. For example, if my mapserver layer piece is as follows:

LAYER                                                                       # start layer 
            CONNECTIONTYPE postgis
            NAME "roads
            CONNECTION "user=username 
            password=**** dbname=databasename host=hostdb.com port=5434" 
            DATA "geom from tablename where ST_Intersects(geom, mapextents)"
            STATUS ON
            TYPE LINE
                    COLOR 0 0 0
END                                                                         # end layer

How can I dynamically supply the value of mapextents to the Mapserver file? I have considered the .map piece usually to be static but perhaps this is incorrect.

An example of the type of query that I want to perform is as follows:

    ST_Clip(r.rast, 'POLYGON((-8293673 4952790,-8137619 4952790,-8137619 5036076, -8293673 5036076,-8293673 4952790))'::geometry)   
    raster AS r                 
    ST_Intersects(r.rast, 'POLYGON((-8293673 4952790,-8137619 4952790,-8137619 5036076, -8293673 5036076,-8293673 4952790))'::geometry)

Secondly, where should I be storing the .map file? At the moment I have them in a web readable folder but I know this is not a good strategy once I need to store a password.

share|improve this question
take a look at variable substitution in Mapserver. –  Lcasagrande Feb 12 '12 at 13:53

1 Answer 1

Mapserver has a flexible interface for dynamically manipulating the map configuration via special parameters in the url. The documentation is available at:


In your case:

 ...&map.extent=123 123 456 567&...

At the end of the same section there is also an example of how to use the same run-time substitution to hide the map file path. Usually you would place the map file in a path is not directly served by apache (ie outside the web root).

Please remember to set the correct permissions on the map file and associated data so that the apache user can access it (read permission is enough).

share|improve this answer
Hi @unicoletti - I updated my question to illustrate the type of spatial query I am describing. I would like this query to work for non-rectangular regions also (though I am unsure if this is possible). –  djq Feb 12 '12 at 22:30
Do you mean clipping? Have a look at this: mapserver.org/development/rfc/ms-rfc-79.html –  unicoletti Feb 13 '12 at 8:57
For example, can I supply a user-draw polygon which is used to clip the raster? I notice the '%token%' parameter in your link - could I set '%token%' to the value of 'POLYGON((..,..,..,..)) ? –  djq Feb 13 '12 at 15:20
Exactly, in your case the polygon could be defined as an inline FEATURE object: mapserver.org/mapfile/feature.html#feature see example 3: mapserver.org/cgi/controls.html#using-mapserver-version-5 –  unicoletti Feb 13 '12 at 19:52
Thanks for the help thus far. So if I wanted to pass a polygon, how would it be structured? I see points are ...&map_layer[3]=FEATURE+POINTS+500000+1000000+END+TEXT+'A+test+point'+END&... but I can't see polygons anywhere (unless I'm missing something). –  djq Feb 13 '12 at 20:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.