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. The Django Book第六章(Admin)随笔

    要使用Django自带的管理界面,首先得激活- 激活的前提首先在你的项目的seeting目录下的INSTALL_APPS必须有以下的的包 django.contrib.admin django.con ...

  2. Java HttpURLConnection 下载图片 图片全是“加密图片”文字,怎么解决?

    package com.qzf.util; import java.io.FileOutputStream;import java.io.IOException;import java.io.Inpu ...

  3. php->是什么意思

    在php中经常会看到这种符号,但是不明白是什么意思,有没有细心点的朋友帮讲讲,请不要说看手册之类的话.如果真给我讲明白了,我还会有加分的哦!比如这个中的符号是什么意思:$sql=$mydami-> ...

  4. k-SLAM:k-mer Sorted List Alignment and Metagenomics

    k-SLAM 是基于大量高通量宏基因组序列数据分析的比对程序,它基于k-mer技术上在reads和序列之间进行比较,然后用Smith-Waterman算法验证.校准是连接在一起组成一个伪组装用来提高特 ...

  5. (19)3 moons and a planet that could have alien life

    https://www.ted.com/talks/james_green_3_moons_and_a_planet_that_could_have_alien_life/transcript00:1 ...

  6. 证明 U and V={0}时 dim(U+V)=dim(U)+dim(V)

    U And V={0} 证明 dim(U+V)=dim(U)+dim(V)设{u1,u2,...,uk} 是U的基,{v1,v2...,vr}是V的基,dim(U)=k ,dim(V)=r dim(U ...

  7. aliyun API 调试

    打开https://ai.aliyun.com/,登录阿里云账号,选择控制台,右侧标签中选择产品服务,选择自己需要的子标签(如图像识别),选择API调试,按要求填写表格. 其中请求Body参照API文 ...

  8. C++IO cin

    cin cin.get() 每次只读缓冲区一个字符,不能接收空格 cin.getline() 读缓冲区一行,能够接收空格 cin.ignore(2) 忽略缓冲器2个字节 int i = cin.pee ...

  9. python 基础_字符串9

    凡是重要的# 字符串的创建,字符串可以是单引号创建也可以是双引号创建 str1 = 'hello world' #当你要输出的是单引号的时候,你括起字符串的必须是双引号.当你输出的是双引号的时候,你括 ...

  10. java调用执行cmd指令启动weblogic

    这里的例子是启动weblogic import java.io.BufferedReader; import java.io.IOException; import java.io.InputStre ...