douping3860 2013-07-31 09:23
浏览 70
已采纳

php算法,从2D平方内收集所有坐标到数组

I have a map with places on it. The coordinates for the places are saved in MySQL as a lat/lon string ("3456,789"). I'm trying to write a function that will give me an array of all coordinates in the shape of a square with my coordinates as the centre-point. I will then hit the database to see if any other places have coordinates in this array.

I can define the corner points of the square and can write a clunky function to fill the array with all the coordinates one by one with a for loop or something like that, but I'm wondering if there is a clever algorithm that can take care of this for me. Example below with expected output:

function getNearbyPlaces($distance = 4){
   $radius = $distance / 2;
   $coords = explode(",",$object->metadata["Coordinates"]["value"]); // array("3456","789")
   $topLeft = array($coords[0] - $radius,$coords[1] - $radius);
   $topRight = array($coords[0] + $radius,$coords[1] - $radius);
   $botLeft = array($coords[0] - $radius,$coords[1] + $radius);
   $topRight = array($coords[0] + $radius,$coords[1] + $radius);

   // calculate all coords within square here

   // Expected output
   // array("3454,787","3455,787","3456,787","3457,787","3458,787","3454,788","3455,788","3456,788","3457,788","3458,788","3454,789","3455,789","3456,789","3457,789","3458,789","3454,790","3455,790","3456,790","3457,790","3458,790","3454,791","3455,791","3456,791","3457,791","3458,791");
}

Does anyone know of a simple way of doing this?

To answer some potential questions early, the coordinates must be stored as lat/lon strings in the database, there is currently no way around this. This has no connection with Google maps.

Many thanks in advance

EDIT Added base SQL query for convenience:

"SELECT * FROM ".$database_table_prefix."user_objects_metadata WHERE..."
// the coordinates are saved in column "value"
  • 写回答

1条回答 默认 最新

  • douguanyun2169 2013-07-31 10:11
    关注

    It seems that what you need is to select all the database points within your range.

    Without the table data it is impossible to write a full SQL query for you, however I had a fiddle and produced the following for you which should convert the strings into floats which you can then compare. It should work if all the strings are the same format, otherwise you can do much the same using instr() to select the location of the comma.

    SELECT CAST( CONCAT_WS('.', LEFT( '1234,567', 4 ), RIGHT( '1234,567', 3 ) ) AS BINARY ) 
    

    With this you might try calculating your expected min and max lat and long in php then plug them in vis:

    SELECT lat,long FROM locations WHERE
    CAST( CONCAT_WS('.', LEFT( lat, 4 ), RIGHT( lat, 3 ) ) AS BINARY ) >= '$minlat' AND
    CAST( CONCAT_WS('.', LEFT( lat, 4 ), RIGHT( lat, 3 ) ) AS BINARY ) <= '$maxlat' AND
    CAST( CONCAT_WS('.', LEFT( long, 4 ), RIGHT( long, 3 ) ) AS BINARY ) >= '$minlong' AND
    CAST( CONCAT_WS('.', LEFT( long, 4 ), RIGHT( long, 3 ) ) AS BINARY ) <= '$maxlong'
    

    I realise this isn't quite what you had in mind in the question but to me it seems the way to go.

    EDIT:

    Coping with the variable precision of the lat and long is done with instr() by calling:

     CAST( CONCAT_WS('.', LEFT( lat, INSTR(lat,',')-1),SUBSTRING( lat, INSTR(lat,',')+1 ) ) AS BINARY )
    

    You do need to use the SUBSTRING() method rather than RIGHT() though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据