{"id":661,"date":"2010-01-28T19:37:48","date_gmt":"2010-01-28T18:37:48","guid":{"rendered":"http:\/\/sickel.net\/blogg\/?p=661"},"modified":"2010-01-30T12:24:02","modified_gmt":"2010-01-30T11:24:02","slug":"query-a-layer-in-mapscript-php","status":"publish","type":"post","link":"http:\/\/sickel.net\/blogg\/?p=661","title":{"rendered":"Query a layer in mapscript php"},"content":{"rendered":"<p>When learning to use mapserver and mapscript, the first steps have been to display, zoom and pan. Zooming and panning is quite easy, it is just a line like<\/p>\n<p>$map->zoompoint($zoomfactor,$clickpoint,$map->width,$map->height,$mapextent);<br \/>\nwhere $map is a mapboject, $zoomfactor is a number telling how many times to zoom in or out, positive for zoom in, negative for zoom out, $clickpoint is a pointobject of the location of the click in image coordinates. <\/p>\n<p>Then enter the urge to do a query as well &#8211; but not much luck. I reused the same $clickpoint and tried<br \/>\n$map->queryByPoint($clickpoint, MS_SINGLE, 1);<br \/>\n(after doing some adjustments in the map file) but to no luck. Digging into the kmap source code, I understood after a while that the coordinates needed to be in map coordinates and not image coordinates. That is just a simple linear transformation, just with the little snag that the y-coordinates go the opposite direction in the image than in the map. Kmap called the transformation function twice, once for each koordinate, I preferred to make one function for the two-dimentional transformation:<\/p>\n<pre>\r\nfunction pix2geo($mapx,$mapy,$minx,$miny,$maxx,$maxy,$sizex,$sizey)\r\n\t\t\t\t\t \r\n\/\/ $mapx and $mapy: image coordinates of point of interest \t\t\t\t \r\n\/\/ $minx, $maxx,$miny, $maxy map extent in map coordinates\r\n\/\/ $sizex and $sizey, width and height of image\r\n{\t\r\n\tlist($miny,$maxy)=array($maxy,$miny); \r\n\t\/\/ swaps miny and max y since the coordinates acsends upwards in the map, downwards in the picture\r\n\t$geopoint=array();\r\n\tforeach( array('x','y') as $dim){\r\n\t\t$p=array();\r\n\t\tforeach(array('map','min','max','size') as $par){\r\n\t\t\t$name=\"$par$dim\";\r\n\t\t\t$p[$par]=$$name;\r\n\t\t}\r\n\t\t$geowidth=$p['max']-$p['min'];\r\n\t\t$geopoint[$dim]=$p['map']\/$p['size']*$geowidth+$p['min'];\r\n\t}\r\n\treturn(array($geopoint['x'],$geopoint['y']));\r\n}\r\n<\/pre>\n<p>This can be used like:<\/p>\n<pre>\r\nlist($geoX,$geoY)=pix2geo($_GET['MAP_x']*1,$_GET['MAP_y']*1,$minx,$miny,$maxx,$maxy,$map->width,$map->height);\r\n$geopoint=ms_newpointObj();\r\n$geopoint->setXY( $geoX,$geoY);\r\n$map->queryByPoint($geopoint, MS_SINGLE, 1);\r\n<\/pre>\n<p>followed by getNumResults for each layer, getResult, getShape and finally getting the relevant values out of the shape.<\/p>\n<p>I have to admit, I simplified things a bit at the beginning. The point needs to be in the projection of the queried data set. As I have the queried dataset in geographical projection and want to have the map in utm, I still have a problem there..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When learning to use mapserver and mapscript, the first steps have been to display, zoom and pan. Zooming and panning is quite easy, it is just a line like $map->zoompoint($zoomfactor,$clickpoint,$map->width,$map->height,$mapextent); where $map is a mapboject, $zoomfactor is a number telling &hellip; <a href=\"http:\/\/sickel.net\/blogg\/?p=661\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-mapserver"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnVtD-aF","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=661"}],"version-history":[{"count":4,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":673,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions\/673"}],"wp:attachment":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}