如何计算出哪个 shapefile(郊区)包含 PHP 中给定的经纬度点

发布于 2024-12-14 03:58:04 字数 217 浏览 3 评论 0原文

我只能访问 PHP5(没有 PostGIS)

我有一堆郊区形状文件,以及一些带有经纬度点的事件。我对 shapefile 的经验为零。

检查哪些 shapefile 包含这些经纬度点的最佳方法是什么(仅使用 PHP)?

我是否将形状文件转换为经纬度多边形并使用标准多边形点交点方程?

或者这些是一些很棒的用于加载/使用 Shapefile 的 PHP 库?

I only have access to PHP5 (no PostGIS)

I have a bunch of suburb shapefiles, and a few events with lat-lon points. I've zero experience with shapefiles.

What is the best way to check which shapefiles contain these lat-long points (using only PHP)?

Do I convert the shapefiles to a lat-long polygon and use standard polygon-point intersection equation?

Or is these some awesome PHP library for loading/working with Shapefiles?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能否归途做我良人 2024-12-21 03:58:04

每个shapefile由3部分组成,shp、shx、dbf。 shp 文件包含几何图形,shx 是帮助访问 shp 的索引,dbf 是包含每个记录的数据的普通旧数据库文件。

您可以按如下方式从 shp 文件中提取边界框,

$handle = fopen("path/to/file.shp","rb");
fseek($handle, 36);
$min_x = unpack("d",fread($handle,8);
$min_y = unpack("d",fread($handle,8);
$max_x = unpack("d",fread($handle,8);
$max_y = unpack("d",fread($handle,8);
// Note, this code will only work on a little-endian machine
// You'll need to do a byte swap on big endian systems

然后可以测试给定事件是否位于 shapefile 的边界框中。

if (($event_x >= $min_x) && ($event_x <= $max_x) 
      && ($event_y >= $min_y) && ($event_y <= $max_y))

您可以将其作为一个循环,并获取与给定事件重叠的 shapefile 的子集。这并不意味着您的事件位于给定形状文件中的多边形内,但它会让您接近。如果您需要精确的解决方案,则必须提取多边形并在多边形测试中进行点。

免责声明:考虑上面的代码伪代码,我不懂php,所以可能存在一些错误。另外,如果您可以切换到 python,事情会变得容易得多,现有的库提供 shapefile 解析和空间索引,因此您可以以高效的方式准确确定点与哪些多边形相交。

参考:ESRI Shapefile 白皮书,http://www.esri.com/library/whitepapers /pdfs/shapefile.pdf

Each shapefile consists of 3 parts, shp,shx,dbf. The shp file contains the geometry, shx is an index to help access the shp, the dbf is plain old dbase file that contains data for each record.

You can extract the bounding box from the shp file as follows,

$handle = fopen("path/to/file.shp","rb");
fseek($handle, 36);
$min_x = unpack("d",fread($handle,8);
$min_y = unpack("d",fread($handle,8);
$max_x = unpack("d",fread($handle,8);
$max_y = unpack("d",fread($handle,8);
// Note, this code will only work on a little-endian machine
// You'll need to do a byte swap on big endian systems

Then you can test to see if a given event lies in the shapefile's bounding box.

if (($event_x >= $min_x) && ($event_x <= $max_x) 
      && ($event_y >= $min_y) && ($event_y <= $max_y))

You can put this is a loop and get a subset of your shapefile that overlap with a given event. This doesn't mean you event is inside a polygon in a given shapefile, but it'll get you close. If you need an exact solution you'll have to extract the polygons and do a point in polygon test.

Disclaimer: Consider the above code pseudo code, I don't know php, so there are probably some bugs. Also, If you can switch to python things get a lot easier, there are existing libraries that provide shapefile parsing and spatial indexing, so you can determine exactly which polygons a point intersects with in a highly efficient manner.

Ref: ESRI Shapefile Whitepaper, http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf

鹿童谣 2024-12-21 03:58:04

要使用 shapefile,我建议将它们加载到具有空间关系的数据库中,并使用数据库的空间关系功能。

我个人为此使用带有 PostGIS 扩展的 PostgreSQL。它有一个用于将 shapefile 转换为 SQL 插入的实用程序。然后,您可以将您的观点放入 WKT(众所周知的文本)中,并在数据库中查询它与哪些 shapefile 相交。

我不相信 php 本身有任何处理 GIS 的内置函数。

编辑-该死的 - 抱歉,直到发帖后我才看到(无 PostGIS)部分。您也许可以将多边形转换为 wkt 并使用多边形点交点。

To work with shapefiles, I suggest loading them into a database with spatial relations, and using the spatial relation capabilities of the database.

I personally use PostgreSQL with PostGIS extension for this. It has a utility for converting the shapefiles into an SQL insert. Then you can put your point into WKT (well known text) and query the database for what shapefile(s) it intersects with.

I do not believe php itself has any built in functions for dealing with GIS.

EDIT- Damn - I'm sorry, I didn't see the (no PostGIS) part until after the post. You might be able to convert your polygons to wkt and use a polygon-point intersection.

恬淡成诗 2024-12-21 03:58:04

我知道这个问题已经很老了,但是作为对社区和未来用户在本机 PHP 中寻找类似功能的服务,我想指出我的 PHP Shapefile 是一个免费开源 PHP 库,可以读取和写入任何 ESRI Shapefile,无需任何第三方依赖。

GitHub 项目链接:https://github.com/gasparesganga/php-shapefile

I know this question is quite old, but as a service to the community and future users looking for similar functionality in native PHP, I'd like to point out that my PHP Shapefile is a free and open source PHP library that can read and write any ESRI Shapefile, without any third party dependency.

Link to GitHub project: https://github.com/gasparesganga/php-shapefile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文