basic programming python: python Shapely Detailed Guide

This article describes the python Shapely Detailed Guide, a friend in need can refer
Shapely is a Python library for geometric object manipulation and analysis of the Cartesian coordinate system.

The introduction of package

from shapely.geometry import Point
 
from shapely.geometry import LineString

There are variables and methods

object.area

  Returns the area (float) of the object.

object.bounds

  返回对象的(minx,miny,maxx,maxy)元组(float类型)

object.length

  返回对象的长度

object.geom_type

  返回对象类型

object.distance(other)

  返回本对象和另一个对象的距离

object.representative_point()

  Returns a cheaply computed point that is guaranteed to be within the geometric object.
object.area

  Returns the area (float) of the object.

object.bounds

  返回对象的(minx,miny,maxx,maxy)元组(float类型)

object.length

  返回对象的长度

object.geom_type

  返回对象类型

object.distance(other)

  返回本对象和另一个对象的距离

object.representative_point()

  Returns a cheaply computed point that is guaranteed to be within the geometric object.
>>> from shapely.geometry import Point
>>> print Point(0,0).distance(Point(0,1))
1.0
>>> from shapely.geometry import LineString
>>> line = LineString([(0,0), (1,1), (1,2)])
>>> line.area
0.0
>>> line.bounds
(0.0, 0.0, 1.0, 2.0)
>>> line.length
2.414213562373095
>>> line.geom_type
'LineString'

Point

class Point(coordinates)

Three kinds of assignments mode

>>> point = Point(0,0)
>>> point_2 = Point((0,0))
>>> point_3 = Point(point)

Have a point object area and length of 0

>>> point.area
0.0
>>> point.length
0.0

Or may be obtained by coords coordinates x, y, z

>>> p = Point(2,3)
>>> p.coords
<shapely.coords.CoordinateSequence object at 0x7ffbc3d60dd0>
 
>>> list(p.coords)
[(2.0, 3.0)]
>>> p.x
2.0
>>> p.y
3.0

coords can be sliced

>>> p.coords[:]
[(2.0, 3.0)]

LineStrings

LineStrings constructor parameter is passed two or more points sequences

LineStrings a target area is zero, non-zero length

>>> line = LineString([(0,0), (0,1), (1,2)])
>>> line.area
0.0
>>> line.length
2.414213562373095

Obtain the coordinates

>>> line.coords[:]
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]
 >>> list(line.coords)
 [(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]

LineString can still accept a different type of object

>>> line2 = LineString(line)
>>> line2.coords[:]
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]

Common format conversion

>>> Point(1,1).wkt
'POINT (1 1)'
>>> Point(1,1).wkb
'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?'
>>> Point(1,1).wkb.encode('hex')
'0101000000000000000000f03f000000000000f03f'
>>> 
>>> Point(1,1).wkb.encode('hex')
'0101000000000000000000f03f000000000000f03f'

Both loads and dumps method

For wkt

>>> from shapely.wkt import dumps, loads
>>> s = dumps(Point(1,2))
>>> s
'POINT (1.0000000000000000 2.0000000000000000)'
>>> ss = loads(s)
>>> ss
<shapely.geometry.point.Point object at 0x7ffbc3d783d0>
>>> ss.coords[:]
[(1.0, 2.0)]

For wkb

>>> from shapely.wkb import dumps, loads
>>> s = dumps(Point(1,2), hex=True)
>>> s
'0101000000000000000000F03F0000000000000040'
>>> ss = loads(s, hex=True)
>>> ss
<shapely.geometry.point.Point object at 0x7ffbc3d78790>
>>> ss.coords
<shapely.coords.CoordinateSequence object at 0x7ffbc3d783d0>
>>> ss.coords[:]
[(1.0, 2.0)]

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data every day to explain the timing of Python programmers technology, and share some learning methods need to pay attention to small detailsHere Insert Picture Description

Published 35 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105029605