Zooming to / highlighting polygon

25 views
Skip to first unread message

Todd Albert

unread,
Aug 30, 2012, 9:32:45 AM8/30/12
to poly...@googlegroups.com
I have a polymaps map where I am overlaying a geoJSON vector layer using TileStache. These vectors are being read from a PostGIS / PostgreSQL database and include a unique ID for each polygon. In a separate database, I have information about each polygon.

The map is setup such that when a user clicks on a polygon, it queries the other database for that id and returns information.

What I want to do is be able to run the query the other way. The user can search the non-spatial database for one of these polygons, too.

QUESTION: Once I have my polygon id, how can I highlight / draw it on the map / center the map to it?

I am thinking that I need to:
1) create a connection to the PG database (using PHP, for example)
2) query the database based on that object's id (return "the_geom"?)
3) add that polygon to the map (duplicating an existing polygon, but now I can style it)

Any help or suggestions?

Thanks,
Todd

Todd Albert

unread,
Aug 30, 2012, 11:42:35 AM8/30/12
to poly...@googlegroups.com
Working on this…

Step one and two are complete. Now I need help with #3. I cannot yet use these data to populate the map with a marker… any suggestions?

Here's how I accomplished the first steps:

First, I needed to install the php5-pgsql drivers (sudo apt-get install php5-pgsql).

Then I use the following php code to connect to the database:

// Connect to PostgreSQL / PostGIS database:

$pgcon = pg_connect ("dbname=gis user=gisuser connect_timeout=5") or die ( 'Can not connect to server' );
if (!$pgcon) {
  echo "No connection to GIS database.\n";
}

// conduct the query (getting X and Y coordinates of CENTROID)

$query2 = "select ST_Y(ST_PointOnSurface(the_geom)),ST_X(ST_PointOnSurface(the_geom)) from parcels where parid='$pcn'";
$result2 = pg_query($pgcon,$query2) or die ( 'Query Error' );
if (!$result2) {
  echo "No query results returned from GIS database.";
}

while ($row2 = pg_fetch_row($result2)) {
  echo "Centroid located at ";
  echo "Latitude: $row2[0]  Longitude: $row2[1]";
  echo "<br />\n";
}
pg_close($pgcon);

Todd Albert

unread,
Sep 21, 2012, 11:49:19 AM9/21/12
to poly...@googlegroups.com
I've figured this out, but since this forum is dead, just contact me if you want a solution.
-Todd
Reply all
Reply to author
Forward
0 new messages