Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: efficient way to measure region properties using shapely

First of all, I apologize to post this easy question. I have a polygon

from shapely.geometry import Polygon

polygon = Polygon([(560023.4495758876400000 6362057.3904932579000000),(560023.4495758876400000 6362060.3904932579000000),(560024.4495758876400000 6362063.3904932579000000),(560026.9495758876400000 6362068.3904932579000000),(560028.4495758876400000 6362069.8904932579000000),(560034.9495758876400000 6362071.8904932579000000),(560036.4495758876400000 6362071.8904932579000000),(560037.4495758876400000 6362070.3904932579000000),(560037.4495758876400000 6362064.8904932579000000),(560036.4495758876400000 6362063.3904932579000000),(560034.9495758876400000 6362061.3904932579000000),(560026.9495758876400000 6362057.8904932579000000),(560025.4495758876400000 6362057.3904932579000000),(560023.4495758876400000 6362057.3904932579000000)])

enter image description here

My goal is compute the minor and the major axis of this polygon, following the Figure example: enter image description here

I find this example in scikit-image but before to use a second module I wish to ask if there is in shapely module a method to calculate these indices.

thanks in advance

like image 376
Gianni Spear Avatar asked Nov 23 '12 22:11

Gianni Spear


People also ask

What is shapely buffer?

When you buffer a geometry, you create a polygon around an existing geometry with an edge at a specified distance from the original geometry. In the case of a point, this results in a circular polygon with radius equal to the distance of the buffer.

What is shapely geometry in Python?

Shapely is a Python package for set-theoretic analysis and manipulation of planar features using (via Python's ctypes module) functions from the well known and widely deployed GEOS library. GEOS, a port of the Java Topology Suite (JTS), is the geometry engine of the PostGIS spatial extension for the PostgreSQL RDBMS.

Is Point in polygon shapely?

There are basically two ways of conducting Point in Polygon queries in Shapely: using a function called . within() that checks if a point is within a polygon. using a function called .


1 Answers

This question is a bit old but I ran into this myself recently, here's what I did:

from shapely.geometry import Polygon, LineString

polygon =  Polygon([(560023.4495758876400000, 6362057.3904932579000000),(560023.4495758876400000, 6362060.3904932579000000),(560024.4495758876400000, 6362063.3904932579000000),(560026.9495758876400000, 6362068.3904932579000000),(560028.4495758876400000, 6362069.8904932579000000),(560034.9495758876400000, 6362071.8904932579000000),(560036.4495758876400000, 6362071.8904932579000000),(560037.4495758876400000, 6362070.3904932579000000),(560037.4495758876400000, 6362064.8904932579000000),(560036.4495758876400000, 6362063.3904932579000000),(560034.9495758876400000, 6362061.3904932579000000),(560026.9495758876400000, 6362057.8904932579000000),(560025.4495758876400000, 6362057.3904932579000000),(560023.4495758876400000, 6362057.3904932579000000)])

# get the minimum bounding rectangle and zip coordinates into a list of point-tuples
mbr_points = list(zip(*polygon.minimum_rotated_rectangle.exterior.coords.xy))

# calculate the length of each side of the minimum bounding rectangle
mbr_lengths = [LineString((mbr_points[i], mbr_points[i+1])).length for i in range(len(mbr_points) - 1)]

# get major/minor axis measurements
minor_axis = min(mbr_lengths)
major_axis = max(mbr_lengths)

Shapely makes it easy to compute the mbr via minimum_rotated_rectangle, but it doesn't appear that the opposite sides are of exact equal length. Because of this, the above calculates the length of each side, then takes the min/max.

like image 128
primitivist Avatar answered Sep 25 '22 10:09

primitivist