[mapserver-users] Re: MapServer dynamic thematicmap

Stephen Woodbridge woodbri at swoodbridge.com
Fri Sep 25 22:58:55 EDT 2009


jxRui,

First, please subscribe to the mapserver-users list and keep the 
discussion there so others can learn from the discussion and so it gets 
archived.

I would take a slightly different approach which I think is easier to 
implement. Create your mapfile LAYER with say 4-5 classes like:

LAYER
   NAME "states"
   TYPE POLYGON
   STATUS DEFAULT
   CONNECTIONTYPE postgis
   CONNECTION "user=postgres dbname=canada host=carto port=5432"
   DATA "the_geom from (select the_geom, gid, name, compute_style(pop, 
'pop') as style from states) as foo using srid=4326 using unique gid"
   CLASSITEM "style"
   CLASS
     EXPRESSION "0"
     COLOR ...
   END
   CLASS
     EXPRESSION "1"
     COLOR ...
   END
   ...
   CLASS  # default color if not caught above
     COLOR ...
   END
END

Then make you SQL classify the polygons in this classes. You could write 
some stored procedures that would help to do the classification. If you 
need to keep MIN, MAX, AVG, STD values for your Attributes, then you 
could write a trigger function that updates these values when records 
are updated. You could store these metadata values in a separate table.

You should be able to change the data statement in mapscript with 
something like:

layerObj.data = "the_geom from (select the_geom, gid, name, 
compute_style(pop, 'pop') as style from states) as foo using srid=4326 
using unique gid";

Although I'm not sure if all layer attributes can be changed via mapscript.

you would have to write the compute_style() function and you can pass in 
a column from your table like pop and for the second argument you might 
want to pass a value like 'pop' so in your stored procedure you can do 
something like:

select colmin, colmax, colavg, colstd from state_metadata where col='pop';

to fetch the metadata values.

Hope this helps,
   -Steve Woodbridge
    http://imaptools.com/

芮建勋 wrote:
> Steve W,
> thanks a lot to you!
> I am simply using PostgreSQL for several months.I know that in the 
> mapfile of MapServer,if we want divide this class(such as population of 
> U.S) into 4 types, then the max ,min value and value range of each sub 
> class must be hard coding clearly into mapfile if we want to draw 
> dynamic map,beacase mapserver must read the mapfile each time.In this 
> case,the dynamic thematic map via MapServer is easy to realize.
> When i import the U.S state polygons into PostgreSQL DB, then i could 
> create one field naming such as population in the table,and the 
> attributes of this field is varying everyday.So the max and min value of 
> this attributes is varying correspondingly.
> My ques is,(1)if the attributes of (population )field changed each 
> time,Should i change the mapfile subclass definition section? This maybe 
> a terrible dream!^_^
>   (2)if not so,that is to say, i can make dynamic SQL including the 
> max,min and range value definition in php mapscript/php context,and 
> there is no need to write these max ,min value and value range of each 
> sub class into mapfile ? And  the mapfile only including connection 
> parameters to DB,no need including the subclass definitio?
>   I found in a book that said,in Python context,we can define class of a 
> layer easyly,since the interface of MapServer to Python have already 
> been defined,but for PHP/Mapscript is hard to realize. Is this the case?
>  These ques trouble me for long time!
>  If you can give me several lines codes of PHP in order to  "can change 
> the DATA statement
> in php mapscript to select the appropriate SQL to render the map 
> thematically".
>  Best regards
>  jxRui
> 
> 
> �������������ᵽ:
>  >From: Stephen Woodbridge <woodbri at swoodbridge.com>
>  >Reply-To: 
>  >To: 芮建勁 <gis at shnu.edu.cn>, mapserver-users <mapserver- 
> users at lists.osgeo.org>
>  >Subject: Re: [mapserver-users] Re: [OSGeo-Discuss] MapServer dynamic thematicmap
>  >Date:Fri, 25 Sep 2009 10:07:53 -0400
>  >
>  >> jxRui wrote:
>  >>> Folks,
>  >>> I know that MapServer can draw dynamic thematic map(choropleth map) via
>  >>> cgi.  I'd like to develope MapServer app via PHP MapScript to realize
>  >>> the dynamic thematic map. But i find that the PHP MapScript interface
>  >>>  cannot do this.
>  >>> For instance, i have only one polygon layer about U.S states,and this
>  >>> layer has 50 fields in the dbf or in MySQL,the most important thing is
>  >>>  that these attributes values varying everyday. When the user select one
>  >>> of the fields from the droplist component of the MapServer web app in 
>  >>> Firefox or ie
>  >>>  explorer,then the dynamic thematic map wil be drawn automaticly with the
>  >>>  selected fields.
>  >>> What can i do this via PHP MapScript ? I need your help. Thank a lot to
>  >>>  you first!
>  >>>   Best regards
>  >>>   jxRui
>  >
>  >I think the first question is where is the state polygon layer stored 
>  >and how is it getting updated?
>  >
>  >I would recommend putting it in a PostgreSQL/PostGIS database because:
>  >
>  >1) it is easy to load a shapefile into the database
>  >2) it is easy to update the attributes asynchronously to the mapping 
>  >without any issues
>  >3) postGIS is well supported and easy to use from mapserver
>  >4) it is the best way to do dynamic thematic maps
>  >
>  >Then in you mapfile you can setup the layer with the appropriate 
>  >connection parameters for postGIS, and you can change the DATA statement 
>  >in php mapscript to select the appropriate SQL to render the map 
>  >thematically.
>  >
>  >-Steve W
>  > 



More information about the mapserver-users mailing list