0

Is it possible to select a polygon from a predefined Lat & Lon programmatically?

eg location point: x='1234' y='5678' intersects polygon '1234', therefore SELECT POLYGON '1234'

So far I have managed to get the lat lon of the drop point:

$( "#cow" ).draggable({ revert: true});
        $( "#map" ).droppable({
        drop: function( event, ui ) {          
    var point = map.getLonLatFromPixel( map.events.getMousePosition(event, ui   ))     
        OpenLayers.Util.getElement("pos").innerHTML = point


        }
        });

Now I just neeed to select the polygon that x and y intersect. Ideas?

2

Figured it out. Here is the code:

  $( "#cow" ).draggable({ revert: true});
    $( "#map" ).droppable({
    drop: function( event, ui ) {
    var farmid = 448; 
    var point = map.getLonLatFromPixel( map.events.getMousePosition(event, ui ) )
    var lon =  point.lon.toFixed(3);
    var lat =   point.lat.toFixed(3);
    var dropPosition = point.lon.toFixed(3) + ' ' + point.lat.toFixed(3);

    $.ajax({
    type: "GET",
    url: "../php/position.php", 
    contentType: "application/json; charset=utf-8",
    data: "{'farmid':'"+ farmid +"', 'position':'"+ dropPosition +"'}",
    success: function(returnval){ 

    var pid = parseInt(returnval); 
    var selectB = new OpenLayers.Control.SelectFeature([farmbounds]);
    for(var f=0;f<farmbounds.features.length;f++) {
                                    if(farmbounds.features[f].fid == 'farmpaddocks.'+pid+'') {
                                selectB.select(farmbounds.features[f]);
break;
  }}
}//success
});//ajax
}
});

And the php file looks like this:

<?php 
$farmid= $_GET["farmid"];
$position = $_GET["position"];
$db = pg_connect("host=localhost dbname=postgis user=aaaaa password=bbbbb" ); 
$sql = "SELECT * FROM public.farmpaddocks WHERE farmid = '$farmid' AND st_contains(farmpaddocks.the_geom,ST_GeomFromText('POINT($position)',3111)) ='t'";
$resultD = pg_query($db,$sql); 
while($rowD=pg_fetch_row($resultD))  
{ 
$paddockid= $rowD[0];
echo $paddockid;
}

?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.