MapServer / MapServer

Source code of the MapServer project. Please submit pull requests to the 'main' branch.
https://mapserver.org
Other
1.01k stars 369 forks source link

PHP SWIG/MapScript Shape Attribute Values and Headers/Titles? #5768

Closed andrewteg closed 5 years ago

andrewteg commented 5 years ago

We're using MS4W ( thanks @jmckenna ) and trying to update some code with the updated SWIG/MapScript to make an identify tool work. Before we had used the associate array of values at https://mapserver.org/mapscript/php/phpmapscript.html#shapeobj to get all the values.

I can repeat this in a non-associative way using numvalues and getValue() from https://mapserver.org/mapscript/mapscript.html#shapeobj to create a numerical array, but I'd like to have an associative array, and don't see anything in the layerObj or shapeObj that lets you access the attribute headers/titles/field names.

Is this missing from the new mapscript or did I miss something or am I going about this the wrong way?

geographika commented 5 years ago

You can get layer field names with a similar approach to shape properties. Using the getItem() method - https://www.mapserver.org/mapscript/mapscript.html#layerobj-methods

getItem( int i ) : string Returns the requested item. Items are attribute fields, and this method returns the item name (field name). The numitems field contains the number of items available, and the first item is index zero.

In Python MapScript code would look like below:

for idx in range(0, layer.numitems):
    layer.getItem(idx)

Unfortunately shapeObjs have no reference to their layers - something I ran into recently in #5765, so you'd have to join the layer items and shape values together yourself.

The code behind https://mapserver.org/mapscript/php/phpmapscript.html#shapeobj does a similar approach - see source. This might be a nice feature for all the MapScripts - adding attributes to a shape when retrieved from a layer (note in the PHP docs it states - set only on shapes obtained from layer->getShape())

This could be added along with #5763 (another PHP only function) in a future MapServer release.

andrewteg commented 5 years ago

Perfect, thanks. Sorry I overlooked "attribute fields" right there in the description. Makes sense to move to the layer object. I created a gist that hooks them together very simply so I hope that's useful for someone taking 3.x code and making it 4.x compatible in the future:

https://gist.github.com/andrewteg/81b8d45948d67003f5f1170e51d66e61

Thanks again, Andrew

geographika commented 5 years ago

No problem. The gist is a nice PHP MapScript example. If you get a chance you should post the link to the mapserver-users list.