[Mapserver-west] A newbie in need

Aaron Racicot aaronr at ecotrust.org
Wed May 10 20:12:06 EDT 2006


Hi there Nathan,

Here are a series of code snippets that might get you going.  The basic
idea is to capture xy on the client side (pixels), process them in php
and translate them to map coordinates (meters), and then connect to the
database and use the coordinates in a query.  I am not sure what you
mean by the OGR part of your question, unless you are talking about
changing the DATA statement for a particular layer to use the user
selected coordinates in PHP, but if that is the case I have an example
of that at the bottom as well.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Client xy capture (filled in dynamic by client app).
+ This is from the html page.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
<input type="hidden" name="imgxy" value="1 1">
<input type="hidden" name="imgbox" value="-1 -1 -1 -1">

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP Extract
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Explode the x,y extents
$box_coords = explode(" ", $http_form_vars["imgbox"]);
$point_coords = explode(" ", $http_form_vars["imgxy"]);

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP click2map
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Convert pixels to map units
function click2map ($map, $click_x, $click_y) {
    $e= &$map->extent;
    $x_pct = ($click_x / $map->width);
    $y_pct = 1 - ($click_y / $map->height);
    $x_map = $e->minx + ( ($e->maxx - $e->minx) * $x_pct);
    $y_map = $e->miny + ( ($e->maxy - $e->miny) * $y_pct);

    return array($x_map, $y_map);
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP Connect to Postgresql
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Now connect to the db
$host_str = "host=localhost";
$db_str = "dbname=test_db";
$usr_str = "user=web_user";
$string = $host_str." ".$db_str." ".$usr_str;
$connection = pg_connect($string);
if (!$connection)
{
  pg_close($connection);
  echo "Error: Cannot connect to the database <br>\n";
}
else
{
  echo "Connected to the Test database <br>\n";
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP DB basic query
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
$result_pg_exec = pg_exec($connection, "select distinct(gsid) from
test0");
if (!$result_pg_exec) {
  printf("<B>%s</B><BR>\n", pg_errormessage());
}
$result_rows = pg_numrows($result_pg_exec);
for ($row_index = 0; $row_index < $result_rows; $row_index++)
  {
    //Fetch each row and work with the contents
    $result_row_value = pg_fetch_row($result_pg_exec, $row_index);
  }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP DB inserting user coordinates
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
$sql_temp = "INSERT INTO data_gen_".$user_data['uname'].
  " VALUES (".$i.",GeometryFromText('POINT(".$point_coords[0]."
".$point_coords[1].")',-1))";
$query_results = pg_exec($connection, $sql_temp);

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP DB query using user cords
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
$sql_temp = "SELECT * FROM data_gen_".$user_data['uname'].
  " WHERE Distance(the_geom,GeometryFromText('POINT(".$point_coords[0]."
".$point_coords[1].")',-1)) < 100";
$query_results = pg_exec($connection, $sql_temp);

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ PHP changing the data statement of a layer to use
+ the coordinates.  Carefull to include the oid and 
+ "as foo" parts of the query.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
$oLayer_data_gen = $oMap->getLayerByName("Fixed Station");
// set the layer data
$oLayer_data_gen->set( "data","the_geom from (SELECT
data_gen_".$user_data['uname'].
  ".oid, data_gen_".$user_data['uname'].".* FROM
data_gen_".$user_data['uname'].
  " where Distance(the_geom,GeometryFromText('POINT(".$point_coords[0].
  " ".$point_coords[1].")',-1)) < 100) as foo USING UNIQUE oid USING
SRID=-1");


Anyway, hope this is along the lines of what you need.  If any parts are
unclear just shout and I can give some explanation.  Hopefully the code
speaks for itself!

Good luck and share back with the group how it goes...

Aaron

+----------------------------------------+
| Aaron Racicot  | aaronr at ecotrust.org   |
| GIS Programmer | 503.467.0759          |
+----------------------------------------+
| e c o t r u s t                        |
| Jean Vollum Natural Capital Center     |
| 721 NW Ninth Avenue                    |
| Suite 200                              |
| Portland, OR 97209                     |
| www.ecotrust.org                       |
+----------------------------------------+


-----Original Message-----
From: Nathan Levesque [mailto:nlevesque at preparedresponse.com] 
Sent: Wednesday, May 10, 2006 8:52 AM
To: Aaron Racicot
Subject: Re: [Mapserver-west] A newbie in need


Hey Aaron, I have decided to use PHP/Mapscipt instead of the CGI version

of Mapserver. You said you had some examples. Does the offer still stand

on those?

Aaron Racicot wrote:
> Hi there Nathan,
> Just catching up on some email and did not see a response to your 
> question.  Did you find an answer?  I usually do this through PHP 
> Mapscript and have plenty of examples I can send you if you need it.  
> If you are using the CGI version of Mapserver I will probably leave 
> the answer with someone else on the list as I tend not to stay very 
> current on that implementation.  Let me know (through the list) if you

> still need help.
>
> A
>
> +----------------------------------------+
> | Aaron Racicot  | aaronr at ecotrust.org   |
> | GIS Programmer | 503.467.0759          |
> +----------------------------------------+
> | e c o t r u s t                        |
> | Jean Vollum Natural Capital Center     |
> | 721 NW Ninth Avenue                    |
> | Suite 200                              |
> | Portland, OR 97209                     |
> | www.ecotrust.org                       |
> +----------------------------------------+
>
>
> -----Original Message-----
> From: mapserver-west-bounces at lists.maptools.org
> [mailto:mapserver-west-bounces at lists.maptools.org] On Behalf Of Nathan

> Levesque
> Sent: Tuesday, April 18, 2006 10:42 AM
> To: mapserver-west at lists.maptools.org
> Subject: [Mapserver-west] A newbie in need
>
>
> Hey there! I pretty new to MapServer and I need some serious help. I 
> am
> trying to capture the mapx and mapy coords and pass those values to an

> OGR connection to Postgres. I need to be able to do a query based on 
> where the user has clicked on the map. Any suggestions?
>
> Thanks
> Nathan
>
>   

-- 

Nathan Levesque
Prepared Response, Inc.
1127 Broadway Plaza, Suite 204
Tacoma, Wa 98402
253-272-1730
STATEMENT OF CONFIDENTIALITY
The information contained in this electronic message and any attachments
to this message are intended for the exclusive use of the addressee(s)
and may contain confidential or privileged information. If you are not
the intended recipient, please notify Prepared Response, Inc.
immediately at (206) 233-5544 and destroy all copies of this message and
any attachments.





More information about the Mapserver-west mailing list