[mapserver-users] Query by attributes with PHP/MapScript

woodbri at swoodbridge.com woodbri at swoodbridge.com
Wed Oct 2 09:30:50 EDT 2002


On 2 Oct 2002 at 11:41, Armin Burger wrote:

> Hi all,
> 
> I would like to query shapefiles based on their attibutes using
> PHP/MapScript, like
> 
>   street='mainstreet' and city='newtown'
> 
> and then get back all records, including for instance the bounding box
> of the shape.

This is basically a geocoding function. I ended up implementing this 
in C for the US Tiger data, because if you have a large dataset it 
could take a long time to query it and requires a lot of slow disk 
reads. As part of this I had to build indexes to make it fast as 
there are about 6500 files that might have to be scanned depending on 
the query!!!

    http://iMapTools.com/demos/?tab=1

> There is the function QueryByAttributes, but as I saw in the mail
> archives you have to set a filteritem and then a filter on the layer.
> That can only use one field as filteritem. I would need 2 or even more
> fields used in the query.
> 
> I was thinking about using basic PHP functions for dBase.
> Unfortunately the dBase support of pure PHP seems to be very
> rudimentary and does not allow queries. There is the Pear DB module,
> but there is no documentation to what extent dBase files are
> supported.
> 
> Can anybody who was succesful in this area give me hints how to
> achieve slightly complex attribute queries with PHP or PHP/MapScript?
> 
> An optimal solution in my opinion would be a query interface in
> MapScript that mimics the SQL syntax. Though I don't have any idea how
> difficult this would be to implement.

While there are some simple SQL parsers available on the web, the 
real problem becomes a) mapping the SQL actions into all the data 
sets that mapserver supports and b) performance of the queries. 
Things like shapefiles do not have indexes to make attribute queries 
fast, so you end up always doing full table scans, or you have to add 
generating indexes, but them you end up building a whole relational 
database on top of structures that are not nicely suited for them :(

Why not load your attribute data into a MySQL or all your data into 
PostGIS and be done with it. then you can make you SQL queries and 
generate appropriate indexes, etc. If you use shapefiles then store 
the shapefile name and the index to the record along with the 
attribute info so you have a back pointer to the spatial data. I 
built the following placename geocoder using Perl to load the data 
into MySQL and PHP as the web UI with over 7 million place names:

    http://iMapTools.com/geocode.php

Any way, using a database should work well from PHP or Perl and you 
can write a script to upload the data into the database.

-Stephen Woodbridge
 http://iMapTools.com

> Armin
> 
> 
> 





More information about the mapserver-users mailing list