Convex Hull and SciPy

2024/10/9 22:23:58

I'm trying to use scipy (0.10.1) for a quick hack to visualize the convex hull.

I can get the convex hull using the following code:

vecs = [[-0.094218, 51.478927], [-0.09348,  51.479364], [-0.094218, 51.478927],...[-0.094218, 51.478927], [-0.094321, 51.479918], [-0.094218, 51.478927],[-0.094222, 51.478837], [-0.094241, 51.478388], [-0.094108, 51.478116],[-0.09445,  51.480279], [-0.094256, 51.478028], [-0.094326, 51.500511]]
hull = scipy.spatial.Delaunay(vecs).convex_hull

the resulting array looks like this:

[[56,  9], [16,  1], [56,  1], [55,  9], [53, 55], [53, 16]]

the numbers are the vertex indices. My problem is they are not ordered. I'd need them to be in CW or CCW order in order to easily visualize them in KML.

Is there any easy way to have scipy.spatial compute the proper clockwise order?

Answer

So this code seems to do the trick, but could be simpler... Essentially, I first collect the vertex numbers from the hull. Then I compute the mean, recenter the dataset and sort it by the angle from the mean.

ps = set()
for x, y in hull:ps.add(x)ps.add(y)
ps = numpy.array(list(ps))
center = vecs[ps].mean(axis=0)
A = vecs[ps] - center
h = vecs[ps[numpy.argsort(numpy.arctan2(A[:,1], A[:,0]))]]
https://en.xdnf.cn/q/69963.html

Related Q&A

Flask Confirm Action

Im creating a site using the Flask framework, and am implementing a confirmation page for (mainly administrative) actions; i.e. deleting a user.My current method (detailed below) works, but feels quite…

Regex for accent insensitive replacement in python

In Python 3, Id like to be able to use re.sub() in an "accent-insensitive" way, as we can do with the re.I flag for case-insensitive substitution.Could be something like a re.IGNOREACCENTS fl…

Python + Flask REST API, how to convert data keys between camelcase and snakecase?

I am learning Python, and coding simple REST API using Flask micro-framework.I am using SQLAlchemy for Object-relational-mapping and Marshmallow for Object-serialization/deserialization.I am using snak…

pytest reports too much on assert failures

Is there a way for pytest to only output a single line assert errors?This problem arises when you have modules with asserts, If those asserts fails, it dumps the entire function that failed the assert…

pulp.solvers.PulpSolverError: PuLP: cannot execute glpsol.exe

I am a newbie with python and optimization. I am getting some error, please help me resolve it. I tried running the below mentioned code in PyCharm where I am running Anaconda 3from pulp import * x = L…

Django urldecode in template file

is there any way do the urldecode in Django template file? Just opposite to urlencode or escapeI want to convert app%20llc to app llc

Structure accessible by attribute name or index options

I am very new to Python, and trying to figure out how to create an object that has values that are accessible either by attribute name, or by index. For example, the way os.stat() returns a stat_resul…

Loading data from Yahoo! Finance with pandas

I am working my way through Wes McKinneys book Python For Data Analysis and on page 139 under Correlation and Covariance, I am getting an error when I try to run his code to obtain data from Yahoo! Fin…

Run Multiple Instances of ChromeDriver

Using selenium and python I have several tests that need to run in parallel. To avoid using the same browser I added the parameter of using a specific profile directory and user data (see below). The p…

numpy 2d array max/argmax

I have a numpy matrix:>>> A = np.matrix(1 2 3; 5 1 6; 9 4 2) >>> A matrix([[1, 2, 3],[5, 1, 6],[9, 4, 2]])Id like to get the index of the maximum value in each row along with the valu…