shapefile的使用和地理信息的获得

Shapefile文件是美国ESRI公司发布的文件格式,因其ArcGIS软件的推广而得到了普遍的使用,是现在GIS领域使用最为广泛的矢量数据格式。官方称Shapefile是一种用于存储地理要素的几何位置和属性信息的非拓扑简单格式。

一般地,Shapefile文件是多个文件的集合,至少包括一个shp,shx以及dbf文件。

shp主文件使用变长记录存储空间几何数据,支持点,线,面等多种几何类型。
shx索引文件用于存储几何数据的索引信息,包含对主文件中每个记录长度的描述(注意不是空间索引)
dbf表文件是使用dBase数据库表文件进行空间属性数据存储的文件
所以,我们如果要自己完全从底层写代码解析Shapefile文件的话,需要根据shx文件中的信息读取shp中的二进制数据并转化为几何对象,然后再读取dbf表格,将属性添加到几何对象上就完成了对一个Shapefile文件的解析.

其实现在,如果给定一个地点的经度和维度,现在让你想判断一下,这个地点是算什么区域,正常做法是,我们调用百度地图或者谷歌地图的API里面的接口就可以了。但是其实,如果当这些接口不能调用的时候,我们该怎么办呢。

这里我们可以考虑下用shapefile来完成这个任务。

1. 首先在shapefile里面,文件数据提供了信息,这个信息可以帮助我们绘制一个地区区域的多边形。

2. 接着我们需要把我们的目标点绘制成一个点

3. 剩下的任务就是,我们来判断,目标区域的点是不是在地形绘制的多边形里面。

判断任务3的时候,我们可以用一个景点的 point in ploygon 理论,大概是这个样子

判断的算法我就直接引用了:

1) Draw a horizontal line to the right of each point and extend it to infinity

1) Count the number of times the line intersects with polygon edges.

2) A point is inside the polygon if either count of intersections is odd or
   point lies on an edge of polygon.  If none of the conditions is true, then 
   point lies outside.

那这样,我们有我们目标点的坐标,我们又有我们区域的大概的形状,那这样我们就可以确定,我们的目标点的所在区域了

下面是代码,用python实现的

# Library
# 这个是用来判断点在不在多边形里面的库,同时绘制点和多边形
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
# 这里是用这个来读取shapefile文件
import shapefile

接着我们读入数据

1 # Load the shapefile information
2 sf = shapefile.Reader("./vic_suburb_boundary/VIC_LOCALITY_POLYGON_shp") # note, no suffix, all 3 files are used
3 recs = sf.records()
4 shapes = sf.shapes()

这时候我们先看下,shapefile里面有哪些信息

recs[0]

这里我们发现,基本上可能是与这个区域有关的一些文字信息,我们看到第7个元素是我们这次需要的,是这个区域的名字,接着我们看下shapes里面的内容

 shapes[0].points 

这里我们发现是一系列坐标点,这些坐标点可以帮助我们来绘制这个区域的的多边形

# Build a list to hold the name of the suburb
subsurb_name = []
for item in recs:
    # Extract the 7th element:subsurb name
    subsurb_name.append(item[6])
# Check the extraction result
print(subsurb_name[:5])

# Build a list to hold the ploygon represent the subsurb
sub_plon = []
for item in shapes:
    # Using the points information to draw the ploygon
    polygon = Polygon(item.points)
    sub_plon.append(polygon)
# Drow one of the subsurb
sub_plon[0]

在上面两步,我们把shape中的区域名字信息以及区域性质信息都提取了出来,接着,我们就可以用这个信息,来判断,我们的目标点,相应都在哪里了

# Combine name and ploygon list together
sub_info = list(zip(subsurb_name,sub_plon))

# lat information for all the lats
lat_list = list(df_house.lat)
# lng information 
lng_list = list(df_house.lng)
# Zip them in the list of tuples
position_list = zip(lng_list, lat_list)

# A list for holding the subsurb information for each house property
sub_for_house = []
# Loop through all the house 
for item in position_list:
    # Build a point to represent the house property
    point = Point(item[0],item[1])
    # Check where the point is located
    for sub in sub_info:
        # Return true if the point is in the ploygon
        if sub[1].contains(point):
            # Collect the result
            sub_for_house.append(sub[0])

# Check the result
sub_for_house[:5]

根据我们之前的算法,我们把每个我们的目标地址都便利了一遍,并且计算出相应的位置点。

这里我们可以验证一下,比如第一个地址,是

“120 Power Road”,根据谷歌地图的搜索,他的区域确实是DOVETON,那和我们计算的结果一致。https://www.google.com/search?q=120+Power+Road&oq=120+Power+Road&aqs=chrome..69i57.540j0j4&sourceid=chrome&ie=UTF-8

猜你喜欢

转载自www.cnblogs.com/chenyusheng0803/p/9862455.html