1.安装及导入(在安装shapely之前一定要先安装geos)

pip install geos
pip install shapely

shapely是专门做图形计算的包,基本上图形线段,点的判断包里都有,实现的几何对象的基本类型是点、曲线和曲面;
from shapely.geometry import Point
from shapely.geometry import LineString
from shapely.geometry.polygon import LinearRing
from shapely.geometry import Polygon

#集合
from shapely.geometry import MultiPoint
from shapely.geometry import MultiLineString
from shapely.geometry import MultiPolygon

from shapely.ops import unary_union
import matplotlib.pyplot as plt

2. 常用几何对象的属性及方法

(1) 点 Point

point = Point((0.0, 1.0))

#一个点的面积和长度均为0
print(point.area)
print(point.length)

#边界 ,返回(minx, miny, maxx, maxy) 元组
print(point.bounds)

#坐标值
print(list(point.coords))
print(point.x)
print(point.y)

#坐标可切片
print(point.coords[:])

构造函数可接受对象类型
Point(point)

(2) 线 LineString

line = LineString([(0, 0), (1, 1)])

#一条线的面积和长度
print(line.area)
print(line.length)

#边界 返回(minx, miny, maxx, maxy) 元组
print(line.bounds)

#坐标值
print(len(line.coords))
print(list(line.coords))

#坐标可切片
print(point.coords[:])

#构造函数可接受对象类型
LineString(line)
LineString([Point(0.0, 1.0), (2.0, 3.0), Point(4.0, 5.0)])

(3)线环 LinearRing

ring = LinearRing([(0, 0), (1, 1), (1, 0)])

#面积,长度
print(ring.area)
print(ring.length)

#边界 返回(minx, miny, maxx, maxy) 元组
print(ring.bounds)

#坐标
print(len(ring.coords))
print(list(ring.coords))

LinearRing(ring)

(4) 多边形 Polygon

polygon = Polygon([(0, 0), (1, 1), (1, 0)])

#面积 ,长度
print(polygon.area)
print(polygon.length)

#边界 返回(minx, miny, maxx, maxy) 元组
print(polygon.bounds)

#坐标 外部与内部
print(list(polygon.exterior.coords))
print(list(polygon.interiors))

coords = [(0, 1), (1, 4), (3, 0), (3,1)]
Polygon(coords)

(5) 集合 collections

几何对象的异构集合可能由一些 Shapely 操作产生,集合可以是同类的(MultiPoint, MultiLineString, MultiPolygon 等)或异类的。
MultiPoint([Point(0, 0), Point(1, 1)])

coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0)),((4, 5), (3, 6))]
MultiLineString(coords)

p1 = Polygon([(0, 1), (1, 4), (3, 0), (3,1)])
p2 = Polygon([(1, 2), (4, 3), (3, 2), (3,4)])
p3 = Polygon([(1, 1), (2, 4), (3, 4), (3,7)])
MultiPolygon([p1, p2, p3])

3.常用的一些方法

(1) object.contains(other)

other里没有点在object的exterior,且other的interior里至少有一个点在object的interior
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.contains(other))

object = Polygon([(0, 0), (0, 1), (1, 1)])
other = Polygon([(0, 1), (1, 2), (0, 0)])
print(object.contains(other))

(2) object.crosses(other)

object的interior与other的interior相交,且不包含他
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.crosses(other))

object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
other = LineString([(0.5, 0.5), (2, 2)])
print(object.crosses(other))

(3) object.disjoint(other)

object的interior和boundary 和other的interior和boundary都不想交 返回True
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.disjoint(other))

object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
other = LineString([(2, 2), (3, 3)])
print(object.disjoint(other))

(4) object.intersects(other)

object的interior或者boundary和other的interior或者boundary相交 返回TRUE
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.intersects(other))

object = Point((0, 0))
other = Point((1, 1))
print(object.intersects(other))

(5) object.overlaps(other)

object的interior或者boundary和other的interior或者boundary相交,且不包含他, 返回TRUE
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.overlaps(other))

object = Point((0, 0))
other = Point((1, 1))
print(object.overlaps(other))

(6) object.touches(other)

other和object至少有一个共同的点,且他们的interior不相交
object = LineString([(0, 0), (1, 1)])
other = LineString([(1, 1), (2, 2)])
print(object.touches(other))

object = Point((0, 1))
other = Point((0, 0))
print(object.touches(other))

(7) shapely.ops.unary_union(geoms) 几何合并

返回多个几何对象合并以后的结果
import matplotlib.pyplot as plt # matplotlib 数据可视化
import geopandas as gpd

poly_union = gpd.GeoSeries([Polygon([(0,0), (0,2), (1,2), (1,3),
(2,3), (2,4), (3, 4), (3, 5), (5, 5), (5, 3), (4, 3), (4, 2),
(3,2), (3,1), (2, 1), (2, 0), (0, 0)])])

poly_union.plot(color = ‘blue’)
plt.show()

(8) buffer() 缓冲区分析

根缓冲区分析方法据指定的距离,在点、线、面几何对象周围建立一定宽度的区域的分析方法

点缓冲区:围绕给定点以指定缓冲距离为半径生成圆形区域

p1 = Point(1, 1)
a = p1.buffer(2)
a

线缓冲区:沿线对象的法线方向,分别向线对象的两侧平移一定的距离而得到两条线,并与在线端点处形成的光滑曲线(或平头)接合形成的封闭区域

line_1 = LineString([(0.1, 0.1), (2, 3)])
b = line_1.buffer(0.5)
b

交并显示

intersection = line_1.intersection(b)
x1, y1 = line_1.xy
x2, y2 = b.boundary.xy
plt.figure()
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()

python之shapely库的使用相关推荐

  1. shapely库的基础学习

    DataWhale组队学习打卡第一阶段内容 本学习笔记为Datawhale开源学习训练营21年4月数据挖掘学习的学习内容,学习链接为:团队学习数据挖掘/智慧海洋 所在学习小组:梅利号 shapely库 ...

  2. Python读取dll库报错:[WinError 126]找不到指定的模块

    问题描述 所做的项目需要调用C编译好的dll动态链接库,一般来说,直接运行 import ctypes dll_read = ctypes.cdll.LoadLibrary('./xxx.dll') ...

  3. python内置库之学习ctypes库(二)

    ctypes库踩坑日记2 一.自己实现一个dll文件,再用python的ctypes库调用思路1更清晰 二.生成dll文件 三.ctypes库调用 一.自己实现一个dll文件,再用python的cty ...

  4. python内置库之学习configparser库(一)

    python内置库之学习configparser库(一) 1.引言 ini文件简介 [节] 键=值 注:节不能重复出现 2.自己封装了一个增删改查的类,可以参考一下 import configpars ...

  5. python 脚本撞库国内“某榴”账号

    其实日常生活中我们的用户名和密码就那么几个,所以这给撞库带来了可能,本文主要给出python脚本撞库的一点粗浅代码.这里只讨论技术本生,代码中某榴的地址也已经改掉,避免被管理员误解禁言等发生,谢谢大家 ...

  6. Python中lxml库的安装(Windows平台)

    之前写过<Python中requests包的安装>,今天我需要安装lxml库,这里我尝试之前安装requests方式,但是没有成功,几经周折,终于总结出来了一个方法,这里拿出来给大家分享. ...

  7. 第四章 python的turtle库的运用

    我们可以尝试用python的自带turtle库绘制一条蟒蛇 首先我们设计一下蟒蛇的基本形状 我们先把这段蟒蛇绘制的实例代码贴出来,各位可以在自己的本地运行一下看看效果,然后我们再继续分析代码: 1 # ...

  8. python相对路径库_如何最简单、通俗地理解Python的搜索路径、相对路径、绝对路径?...

    目录: 一.笔记 二.我的自学路线 三.笔记目录 一.笔记 1) 搜索路径 ① 能导入模块的话,表示搜索路径中有这个模块文件. ② 当你导入一个模块,Python解析器对模块位置的搜索顺序是:1. 当 ...

  9. python tkinter库、添加gui界面_使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二)...

    使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二),创建一个,界面,布局,文件,路径 使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二) ...

最新文章

  1. Zeal编程查询,离线文档浏览器
  2. JavaScript设计的漂亮的玫瑰花
  3. Angular jasmine spyOn函数的实现原理
  4. (需求实战_02)_ftp连接下载指定.zip类型文件
  5. linux 访问共享内存,Linux下的共享内存(03)---通过指针访问共享内存中的数据...
  6. springcloud服务发现
  7. CIRCOS增加热图、点图、线图和区块属性
  8. asp.net excel导入 wps_4种Excel格式的转换方法,总有一种适合你!赶紧试试看
  9. c语言模拟键盘自动按键,C语言实现模拟键盘按键事件
  10. 松散四叉树+网格法实现
  11. RS码在AWGN信道和2PSK调制下的误码率曲线图(修改自lin_yulin,亲测可用)
  12. java转正自我陈述_试用期转正个人工作述职报告合集
  13. php字面量,浅谈js之字面量、对象字面量的访问、关键字in的用法
  14. js 获取设备或浏览器唯一标识的方式
  15. Win10命令大全通用
  16. Android 10获取手机相册照片变成白色空白
  17. mos管结电容等效模型_详解各元器件等效电路_电阻、电容、电感、二极管、MOS管...
  18. 浅谈ArcGIS中的容差和分辨率
  19. 计算机公共课1-信息技术与计算机文化
  20. Android UI系列 - 布局 - 属性详解

热门文章

  1. 华硕fl5600l装固态并重装系统到固态
  2. Pico VR 应用开发基础教程
  3. 浅谈JavaScript、ES5、ES6 ,,转自http://www.cnblogs.com/lovesong/p/4908871.html
  4. 逻辑思维训练——假设法
  5. 基于JavaSSM和微信小程序的智能二维码门禁管理系统
  6. 2021-04-09
  7. Android 中 使用 Google Paly 支付 简介
  8. 打印网页去掉页眉和页脚
  9. MIUI9开发版提前完成全系机型适配,近50款小米手机可升级
  10. php文件对应的模板,wordpress模板文件对应说明关系(wp模板文件说明)