English 中文(简体)
我如何利用我方圆环,而不是一平方?
原标题:How do I draw a mysql polygon circle, and not a square?

以下职能在4个点中形成一个多角广场,我假定最后5点已经关闭,我至少需要24点,组成一个圈子。 任何想法?

function getRadius($point="POINT(-29.8368 30.9096)", $radius=2)
{
    $km = 0.009;
    $center = "GeomFromText( $point )";
        $radius = $radius*$km;
        $bbox = "CONCAT( POLYGON(( ,
                X($center) - $radius,    , Y($center) - $radius,  , ,
                X($center) + $radius,    , Y($center) - $radius,  , ,
                X($center) + $radius,    , Y($center) + $radius,  , ,
                X($center) - $radius,    , Y($center) + $radius,  , ,
                X($center) - $radius,    , Y($center) - $radius,  
        )) )";

    $query = $this->db->query("
        SELECT id, AsText(latLng) AS latLng, (SQRT(POW( ABS( X(latLng) - X({$center})), 2) + POW( ABS(Y(latLng) - Y({$center})), 2 )))/0.009 AS distance
        FROM crime_listing
        WHERE Intersects( latLng, GeomFromText($bbox) )
        AND SQRT(POW( ABS( X(latLng) - X({$center})), 2) + POW( ABS(Y(latLng) - Y({$center})), 2 )) < $radius
        ORDER BY distance
                ");

    if($query->num_rows()>0){
                return($query->result());
        }else{
                return false;
        }
}

低于js版本,这项工作是完美的。

var findCirclePolygons = function(point, r)
{   
    var d2r = Math.PI / 180;
    this.circleLatLngs = new Array();
    numPoints = 24;
    var circleLat = r * 0.009;  // Convert degrees into km
    var circleLng = circleLat / Math.cos(point.lat() * d2r);
    for (var i = 0; i < numPoints + 1; i++) {
        var theta = Math.PI * (i / (numPoints / 2));
        var vertexLat = point.lat() + (circleLat * Math.sin(theta));
        var vertexLng = parseFloat(point.lng()) + parseFloat(( circleLng * Math.cos(theta)));
        var vertextLatLng = new google.maps.LatLng(vertexLat, vertexLng);
        this.circleLatLngs.push(vertextLatLng);
    }

    // Set options
    var options = {
        paths: circleLatLngs,
        strokeColor: "#0055ff",
        strokeOpacity: 1,
        strokeWeight: 1,
        fillColor: "#0055ff",
        fillOpacity: 0.35
        };

    // Return
    return options;
};
问题回答

你们可以做一整 lo(在假冒法典中):

for(i=0; i <= 360; i += 360/24)
{
    $extra_point = "POINT(". $radius*cos(i)." ". $radius*sin(i) . ")"
}

http://www.ohchr.org。

  • However, if you just want a bounding box, then using a square is the right way to do it. No need to have a circle looking bounding box. You filter out later with the sorting.
  • To measure the distance, no need to use ABS. Squaring does it for you (as an extra, compare to the radius squared instead of computing the square root if you are extreamly picky about performances
  • Don t forget to add the spatial index or the performances will be crap

2nd EDIT In somewhat less pseudo-code ;) (It s been ages I didn t work with php).

$lon = 42;
$lat = 2;
$radius = 0.01;
$bbox = "POLYGON((";
for(i=0; i <= 360; i += 360/24)
{
    $bbox .= $radius*cos(deg2rad(i)) + $lon." ". $radius*sin(deg2rad(i)) + $lat;
    if(i < 360)"
        $bbox .= ", "
}
$bbox .= "))"

关于距离问题,你有 any(x,2)= any(x,2)。 因此,你只能写(简便):

SQRT(POW( X(latLng) - X({$center}), 2) + POW(Y(latLng) - Y({$center}), 2 )) < $radius

或者甚至要避免计算平方根(在这种情况下,它绝对不会有任何变化,早期优化是坏事,正如你所想的那样。)

POW( X(latLng) - X({$center}), 2) + POW(Y(latLng) - Y({$center}), 2 ) < POW($radius, 2)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签