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

接着我们读入数据

 # Load the shapefile information
sf = shapefile.Reader("./vic_suburb_boundary/VIC_LOCALITY_POLYGON_shp") # note, no suffix, all 3 files are used
recs = sf.records()
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

shapefile的使用和地理信息的获得的更多相关文章

  1. GIS数据格式:Shapefile

    转自:http://lab.osgeo.cn/2449.html Shapefile是ESRI提出的数据格式,随着ArcView GIS 3.x发布,属于简单要素类.Shapefile由于其数据结构简 ...

  2. 如何区分Shapefile,Coverage,Geodatabase(转载)

    转自:http://www.cnblogs.com/linhugh/archive/2012/04/06/2435266.html 在过去20年中,矢量数据模型是GIS中变化最大的方面,例如,ESRI ...

  3. 使用OpenGL绘制 shapefile文件 完成最基本的gis操作

    主要内容概述 (视频教程已经发布:http://edu.csdn.net/course/detail/3422) (http://edu.csdn.net/course/detail/3420) 1. ...

  4. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

  5. shapefile 输出的地理处理注意事项

    多年来,ESRI 为存储地理信息开发了三种主要数据格式 - coverage 格式.shapefile 格式及地理数据库格式.其中,所开发的 Shapefile 为存储地理及属性信息提供了一种简单的非 ...

  6. GeoJson格式与转换(shapefile)Geotools

    转自:https://blog.csdn.net/cobramonkey/article/details/71124888 作为大数据分析的重要工具,Hadoop在这一领域发挥着不可或缺的作用.有些人 ...

  7. shapefile 输出的地理处理注意事项(转载)

    来源:http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//005600000013000000 多年来,Esri 为存储地理信息 ...

  8. AE创建一个空白的Shapefile

    1.IField和IFieldEdit区别: IFieldEdit是继承IField的,因为IField的属性大部分是只读的(read-only),所以IFieldEdit就在IField的基础上多了 ...

  9. AE开发中对GDB以及shapefile的读取、对FeatureClass的相关操作

    读取gdb方法 private void btn_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBro ...

随机推荐

  1. AJAX-URL-HTTP协议-缓存-DOM操作-HTML元素事件

    1.URL 1.URL的作用 用于来表示任意一个资源的位置(互联网上). 协议+主机名+文件目录结构+文件名称 2.详解 格式: <scheme>://<user>:<p ...

  2. KBMMW 4.84.00 发布

    kbmMW is a portable, highly scalable, high end application server and enterprise architecture integr ...

  3. 682. Baseball Game

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  4. 爬取微博的数据时别人用的是FM.view方法传递html标签那么jsoup怎么解析呢

    使用JSOUP就行 这里给出点思路 我只做了自己的首页和其他人的微博首页的抓取 其他的抓取没尝试(不好意思 比较懒...) 首先是利用JSOUP进行登陆 获取页面 看了下微博的登陆表格 发现用了aja ...

  5. jrebel热部署

    一,JRebel 插件 获取与安装 1,JRebel 官网下载地址https://zeroturnaround.com/software/jrebel/download/#!/free-trial P ...

  6. python的6种基本数据类型--集合

    特征 1.确定性(元素必须可hash) 2.互异性(去重) 3.无序性(集合中的元素没有顺序,先后之分) >>> s = {1,1,1,2,2,3,4,5,6,7} # 创建 > ...

  7. js短信验证码

    短信验证码,无注释,url顺便写的错的,所以会报错 <!DOCTYPE html> <html> <head> <meta charset="UTF ...

  8. Redis基于eval的多字段原子增量计算

    目录 目录 1 1. 前言 1 2. 优点 1 3. 方法一:使用struct 2 3.1. 设置初始值(覆盖原有的,如果存在) 2 3.2. 查询k1的值 2 3.3. 设置初始值(覆盖原有的,如果 ...

  9. Mapnik

    Downloads Latest Release The latest release is Mapnik v3.0.22.最新版本是Mapnik v3.0.22. Mapnik 3.0.22 Rel ...

  10. Shell编程-08-Shell中的循环语句

    目录 while语句 until语句 for语句 select语句 循环中断控制 循环语句总结     循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行.在Shell中常见的 ...