[mapserver-users] Re: MapServer dynamic thematicmap

Stephen Woodbridge woodbri at swoodbridge.com
Sun Sep 27 07:14:43 PDT 2009


Correct, the idea behind writing a stored procedure is the you have many 
columns that you want to be able to classify and each column would have 
different ranges for the classification based on the MIN, MAX, AVG 
values. If so you could write a single function in pseudo-code like:

classify_quartile(column, 'column_name') {
    select cmin, cmax, cavg into cmin, cmax, cavg
      from column_metadata where colname='column_name';
    quartile := (cmax - cmin) / 4;
    return column/quartile;
}

or you could write multiple function with different classifiers that you 
could select from a drop down list.

If all the classifiers return values like 1,2,3,4,... the you only need 
a single layer in the mapfile that maps those style values to colors. Or 
you can get a little fancier and you can have you classifier functions 
return the color directly and have mapserver use that.

Look at the mapfile reference for specifying a color attribute column.

-Steve

芮建勋 wrote:
> Stephen Woodbridge,
> Thanks a lot!
> The mapfile i create for mapserver includings polygon LAYER of 
> population with 4 classes like:
> 
> LAYER
>   NAME "MyPOP"
>   TYPE POLYGON
>   CONNECTIONTYPE postgis
> CONNECTION "user=postgres dbname=canada host=carto port=5432"
>  DATA "the_geom from (select the_geom, gid, name, compute_style(pop, &
> apos;pop') as style from states) as foo using srid=4326 using 
> unique gid"
>    CLASSITEM "POP"
>      CLASS
>       EXPRESSION ([POP] <12000 )
>       STYLE
>           COLOR 255 255 255
>          END
>      END
>     
>      CLASS
>       NAME "12000 - 33990"
>       EXPRESSION (([POP] > 12000) && ([POP] <= 33990))
>          STYLE
>           COLOR 223 184 230
>          END
>      END
>  CLASS
>       NAME "33990 - 184253"
>       EXPRESSION (([POP] >= 33990.0001) && ([POP] <= 184253))
>          STYLE
>           COLOR 199 141 235
>          END
>      END
>   CLASS
>       NAME ">184253"
>       EXPRESSION ([POP] >= 184253.0001)
>          STYLE
>           COLOR 172 102 237
>          END
>      END
>   END
> 
>  When mapserver starts to work ,it will read this mapfile firstly. If i 
> want to render dynamic thematic map , according to your suggestion, i 
> 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";
> In this case,that is to say ,there is no need to write hard coding for 
> the four subclass into mapfile(maybe theses coding lines of  four 
> subCLASS should be deleted),since i can write dynamic SQL in MapScript 
> context(layerObj.data,... etc).
> 
> Can i draw following conclusions according to your suggestion:
> 1.using some stored procedures and compute_style() function that would 
> help to do the classification.
> 2.if i want to render dynamic thematic map, i can change the DATA 
> statement in php mapscript to select the appropriate SQL to render the 
> map thematically.
> 
> Best Regards
> Dr. Rui Jianxun
> Owner of www.svgis.com <http://www.svgis.com>
> Lab of Urban Ecology and Environment Restore
> Shanghai Normal University
> 100 Guilin Road, Shanghai 200234, China
> Email:  gis at shnu.edu.cn <mailto:gis at shnu.edu.cn>    ruijianxun at 126.com 
> <mailto:ruijianxun at 126.com>
> 
> 
> 
> �������������ᵽ:
>  >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 dynamic thematicmap
>  >Date:Fri, 25 Sep 2009 22:58:55 -0400
>  >
>  >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