Is there a way to crop/set a scipy Voronoi diagram? - python

I've implemented the Sibon/natural neighbor spatial interpolation using scipy's Voronoi and Shapely's polygons.
But I need to restrict my voronoi diagram as there are some polygons that are unrealistically extended way out of the domain I'm interpolating in which causes inaccuracies. Also there are non-finite regions in the Voronoi diagram for the outer points which I'd rather not have.
I often see that the Voronoi diagram is bounded in a rectangular box but I don't know how to implement it. I have tried to look for it in the scipy documentation without success.
Edit: found how to do it thanks to this post:Colorize Voronoi Diagram
Thanks to anyone that took the time to read and/or write.

Related

Is it an orthogonal projection in the Nearside Perspective?

I'm currently working on world map visualizations. For now, I can use a home-made software for visualizations and point projections (Java), but I would like to upgrade the soft to be able to use a similar tool in Python.
Thus, I wanted to use cartopy with the module PROJ4, not to re-code everything, and use the wonderfull existing libraries.
It perfectly works for the PlateCarree projection, but when I want to use the Nearside Perspective, I observe a small difference between the two methods.
The two following pictures are extracted from the Java software (1) and the cartopy plot (2).
Cartopy (0.17) is used with matplotlib (3.0.2) and proj4 (4.9.1). In both pictures, we are observing at lon=lat=0° and at 400 km.
Here is the first image (Java):
Java visualization
Here is the second one (Cartopy):
Cartopy representation
As one can observe, lands are over-represented in the cartopy plot. Asuming that I want to get exactly the same projection as the one in my Java software (same representation as the "TrueView angles" in Telecom fields), I discovered in the cartopy crs module:
class NearsidePerspective(_Satellite):
"""
Perspective view looking directly down from above a point on the globe.
In this projection, the projected coordinates are x and y measured from
the origin of a plane tangent to the Earth directly below the perspective
point (e.g. a satellite).
"""
So I got this question: which projection is this about? Are the angles kept, which would means that I have an undetected problem? Or is it an orthogonal projection on the tangent plane? In this case, angles are not conserved, and I would need a solution to apply another projection (the correct one in my case). I might use the wrong one...
Thanks for your time,
Lou
I'm not sure if it's an orthogonal projection, but what CartoPy is using is directly from Proj4:
https://proj4.org/operations/projections/nsper.html
I think coordinates in this Nearside Perspective coordinates are Cartesian distances (distances from the origin on a plane), not angles. It sounds like angles are what's being used for your projection. Have you looked at using the Geostationary projection, but with a different satellite height?
https://scitools.org.uk/cartopy/docs/latest/crs/projections.html#geostationary
I can say that in this projection, the coordinates are angles (multiplied by the satellite height). Might be what you're looking for.

SciPy - Delaunay for creating a simple graph of geographic points with latitude and longitude

I have a dataset of Points of Interest (via latitude/longitude) and want to generate a graph out of them. The aim is to have a routeable graph. So I want to have the "nearest" POIs to be connected via an edge.
I came up with Delaunay, cause this will create a simple planar graph out of the points. As far as good, i got some results. But the problem is, the edges are not good connected, due to the fact that the earth is not flat. In the northern and southern hemisphere the triangles are vertically strechted.
Is there a way to use the scipy.spatial.Delaunay package in order to accept latitude / longitude as positioning information, instead having a flat area?
Or does it make sense to use another procedure? Would be great to have some solutionen for python.
I got it running. I read this article and did the first steps to create the delaunay.
The main point is to make a stereographic projection and afterwards the regular 2D delaunay triangulation.
Now i got a nice basic graph.

Create a triangular mesh using Delaunay methods

I'm trying create a triangular mesh using python.
As I know the boundary points, I think the delaunay method is more appropriated.
I tried use scipy. the code is very simple
from scipy.spatial import Delaunay
pixelpoints = np.transpose(np.nonzero(binaryImage))
tri = Delaunay(pixelpoints)
import matplotlib.pyplot as plt
plt.triplot(pixelpoints[:,0], pixelpoints[:,1], tri.simplices.copy())
plt.plot(pixelpoints[:,0], pixelpoints[:,1], 'o')
plt.show()
But I don't want this. I'd like to mesh inside the image bounds. Also, I don't want to mesh inside the holes.
Can I control the number of triangles to cover a surface?
Is there an alternative way to do this?
Thank you.
You can easily remove additional triangles using the Polygon.IsPointInside(tCentroid) where tCentroid is the triangle centroid. IsPointInside() can be derived by this: http://geomalgorithms.com/a03-_inclusion.html.
The Triangle program supports both these needs: refining triangles to a prescribed size and removing triangles outside the polygon or in holes. There seems to be a python interface floating around: the API describes how to specify holes and a maximum triangle area.

Interpolation with Delaunay Triangulation

Having a cloud point shaped like some sort of distorted paraboloid, I would like to use Delaunay Triangulation to interpolate the points. I have tried other techniques (f.ex. splines) but did not manage to enforce the desired behavior.
I was wondering if there's a quick way to use the results of scipy.spatial.Delaunay in a way where I can give the (x,y) coords and get the z-coord of the point on the simplex (triangle).
From the documentation looks like I can pull out the index of the simplex but I am not sure how to take it from there.
You can give the Delaunay triangulation to scipy.interpolate.LinearNDInterpolator together with the set of Z-values, and it should do the job for you.
If you really want to do the interpolation yourself, you can build it up from find_simplex and transform.

How interpolate 3D coordinates

I have data points in x,y,z format. They form a point cloud of a closed manifold. How can I interpolate them using R-Project or Python? (Like polynomial splines)
It depends on what the points originally represented. Just having an array of points is generally not enough to derive the original manifold from. You need to know which points go together.
The most common low-level boundary representation ("brep") is a bunch of triangles. This is e.g. what OpenGL and Directx get as input. I've written a Python software that can convert triangular meshes in STL format to e.g. a PDF image. Maybe you can adapt that to for your purpose. Interpolating a triangle is usually not necessary, but rather trivail to do. Create three new points each halfway between two original point. These three points form an inner triangle, and the rest of the surface forms three triangles. So with this you have transformed one triangle into four triangles.
If the points are control points for spline surface patches (like NURBS, or Bézier surfaces), you have to know which points together form a patch. Since these are parametric surfaces, once you know the control points, all the points on the surface can be determined. Below is the function for a Bézier surface. The parameters u and v are the the parametric coordinates of the surface. They run from 0 to 1 along two adjecent edges of the patch. The control points are k_ij.
The B functions are weight functions for each control point;
Suppose you want to approximate a Bézier surface by a grid of 10x10 points. To do that you have to evaluate the function p for u and v running from 0 to 1 in 10 steps (generating the steps is easily done with numpy.linspace).
For each (u,v) pair, p returns a 3D point.
If you want to visualise these points, you could use mplot3d from matplotlib.
By "compact manifold" do you mean a lower dimensional function like a trajectory or a surface that is embedded in 3d? You have several alternatives for the surface-problem in R depending on how "parametric" or "non-parametric" you want to be. Regression splines of various sorts could be applied within the framework of estimating mean f(x,y) and if these values were "tightly" spaced you may get a relatively accurate and simple summary estimate. There are several non-parametric methods such as found in packages 'locfit', 'akima' and 'mgcv'. (I'm not really sure how I would go about statistically estimating a 1-d manifold in 3-space.)
Edit: But if I did want to see a 3D distribution and get an idea of whether is was a parametric curve or trajectory, I would reach for package:rgl and just plot it in a rotatable 3D frame.
If you are instead trying to form the convex hull (for which the word interpolate is probably the wrong choice), then I know there are 2-d solutions and suspect that searching would find 3-d solutions as well. Constructing the right search strategy will depend on specifics whose absence the 2 comments so far reflects. I'm speculating that attempting to model lower and higher order statistics like the 1st and 99th percentile as a function of (x,y) could be attempted if you wanted to use a regression effort to create boundaries. There is a quantile regression package, 'rq' by Roger Koenker that is well supported.

Categories