Shapely: Difference between revisions
From charlesreid1
(Created page with "Shapely is a library for drawing shapes in Python. Link: https://pypi.python.org/pypi/Shapely =Installing= ==Mac OS X== First install geos, to enable linking shapely with...") |
No edit summary |
||
| Line 38: | Line 38: | ||
Successfully installed shapely-1.6b2 | Successfully installed shapely-1.6b2 | ||
</pre> | </pre> | ||
=Using= | |||
To use, see documentation for basics: https://pypi.python.org/pypi/Shapely | |||
==Points== | |||
To create an approximately circular patch, start by creating a point where the patch will be centered. By setting the point coordinates, you set the patch location. By setting the buffer, you set the radius of the circular patch. | |||
<pre> | |||
>>> from shapely.geometry import Point | |||
>>> patch = Point(0.0, 0.0).buffer(10.0) | |||
>>> patch | |||
<shapely.geometry.polygon.Polygon object at 0x...> | |||
>>> patch.area | |||
313.65484905459385 | |||
>>> from math import pi | |||
>>> pi*10.0*10.0 | |||
314.1592653589793 | |||
</pre> | |||
The area of the patch is approximately <math>\pi r^2</math>, where r is the patch buffer size. | |||
===Dissolve=== | |||
===Intersect=== | |||
==Integrating with GiS Packages== | |||
To integrate Shapely with other Python GIS packages, you can load from and dump to JSON files. | |||
<pre> | |||
>>> import json | |||
>>> from shapely.geometry import mapping, shape | |||
>>> s = shape(json.loads('{"type": "Point", "coordinates": [0.0, 0.0]}')) | |||
>>> s | |||
<shapely.geometry.point.Point object at 0x...> | |||
>>> print(json.dumps(mapping(s))) | |||
{"type": "Point", "coordinates": [0.0, 0.0]} | |||
</pre> | |||
=Flags= | |||
{{PythonFlag}} | {{PythonFlag}} | ||
Revision as of 20:29, 16 April 2017
Shapely is a library for drawing shapes in Python.
Link: https://pypi.python.org/pypi/Shapely
Installing
Mac OS X
First install geos, to enable linking shapely with geographic libraries/coordinate systems:
$ brew install geos
==> Downloading https://homebrew.bintray.com/bottles/geos-3.6.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring geos-3.6.1.sierra.bottle.tar.gz
==> Caveats
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/charles/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/charles/Library/Python/2.7/lib/python/site-packages/homebrew.pth
==> Summary
🍺 /usr/local/Cellar/geos/3.6.1: 401 files, 7.2MB
Next, install shapely with Numpy add-ons to make it faster:
$ pip install shapely[vectorized]==1.6b2
Collecting shapely[vectorized]==1.6b2
Downloading Shapely-1.6b2-cp27-cp27m-macosx_10_6_intel.whl (2.2MB)
100% |████████████████████████████████| 2.2MB 263kB/s
Requirement already satisfied: numpy; extra == "vectorized" in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from shapely[vectorized]==1.6b2)
Installing collected packages: shapely
Successfully installed shapely-1.6b2
Using
To use, see documentation for basics: https://pypi.python.org/pypi/Shapely
Points
To create an approximately circular patch, start by creating a point where the patch will be centered. By setting the point coordinates, you set the patch location. By setting the buffer, you set the radius of the circular patch.
>>> from shapely.geometry import Point >>> patch = Point(0.0, 0.0).buffer(10.0) >>> patch <shapely.geometry.polygon.Polygon object at 0x...> >>> patch.area 313.65484905459385 >>> from math import pi >>> pi*10.0*10.0 314.1592653589793
The area of the patch is approximately $ \pi r^2 $, where r is the patch buffer size.
Dissolve
Intersect
Integrating with GiS Packages
To integrate Shapely with other Python GIS packages, you can load from and dump to JSON files.
>>> import json
>>> from shapely.geometry import mapping, shape
>>> s = shape(json.loads('{"type": "Point", "coordinates": [0.0, 0.0]}'))
>>> s
<shapely.geometry.point.Point object at 0x...>
>>> print(json.dumps(mapping(s)))
{"type": "Point", "coordinates": [0.0, 0.0]}
Flags
| Python a powerful programming language
Scientific Python: Data analysis libraries: Scipy · Numpy · Pandas · Statsmodel Machine learning libraries: Sklearn Neural network libraries: Tensorflow · Keras Plotting/viz: Matplotlib · Seaborn · Jupyter Solving partial differential equations and bessel functions: Fipy · Bessel Functions
Web and Networking Python: Web programming: Flask · Webapps · Mechanize · Scrapy · Gunicorn Wifi: Wireless/Python · Scapy IPython and Jupyter: Jupyter
Drawing, Geometry, and Shapes: Shapely (for drawing shapes): Shapely Geography library: Geos
General Useful Python Utilities: Python Remote Objects: Pyro Logging (create multi-channel log messages): Logging Keyboard (control keyboard from Python): Keyboard
Black Hat Python: Network scanning: Python/Scanner
|