I have no real experience with GIS data, so when what I believed to be a simple problem has turned out to have more subtleties to it, I am dangerously unprepared!
I want to be able to classify a GPS position as inside/outside a polygon defined by GPS co-ordinates. It turns out this is the well-known (but not to me) point-in-polygon problem. I have read many questions/answers on https://gis.stackexchange.com/ (and here e.g. this).
Shapely seems a good solution, but assumes the co-ordinates are on the same cartesian plane, i.e. not GPS? So I would first need to transform my GPS points to UTM points.
Do I need to introduce this extra step, however, if the points being compared (i.e. the point and the polygon) are always going to be naturally within the same UTM zone. They should always be within the same town/city, so can I just leave them as GPS and use the lat/long co-ordinates in Shapely?
I also came across this UTM-WGS84 converter so I could convert my lat/long pairs using this package, and then use those UTM pairs in Shapely, but I would like to avoid any extra dependencies where possible.
Point-in-polygon already assumes a 2D restriction, and GPS coordinates are 3D. Right away, that gets you in trouble.
A simple workaround is to discard the GPS height, reducing it to 2D surface coordinates. Your next problem is that that your 2D surface is now a sphere. On a spherical surface, a polygon divides the surface in two parts, but there is no obvious "inside". There's a left-hand side and a right-hand side which follows from the order of points in the polygon, but neither side is the obvious "inside". Consider the equator as a trivial polygon - which hemisphere is "inside" the equator?
Next up is the issue of the polygon edges. By definition, these are straight, i.e. line segments. But lines on a spherical surface are weird - they're generally known as great circles. And any two great circles cross in exactly two points. That's not how cartesian lines behave. Worse, the equations for a great circle are not linear when expressed in GPS coordinates, because those are longitude/latitude pairs.
I can imagine that at this point you're feeling a bit confused. You might want to look at this from another side - we have a similar problem with maps. Globe maps are by definition attempts to flatten that non-flat surface. Since that's not exactly possible, you end up with map projections. You can also project the corner points of your polygons on such projections. And because the projections are flat, you can draw the edges on the projection. You now see the problem visually: On two different projections, identical polygons will contain different parts of the world!
So, since we agreed that in the real world, the edges of the polygons are great circles, we should really consider a projection that keeps the great circles straight. There's exactly one family of projections that has this property, and that's the Gnomonic projection. It's a family of projections because you can pick any point as the center.
As it happens, we have one natural point to consider here: the GPS point we're considering. If you put that in the center, draw a gnomonic projection around it, project the polygon edges, and then draw the polygon, you have an exact solution.
Except that the actual earth isn't spherical. Sorry. How exact did you need the test to be, anyway?
Related
I have the coordinates of the outline of a (convex) quadrilateral. These are stored as a list in the form of outline = [(x1,y1), (x2,y2), ..., (xn,yn)], though this can be changed/modified in any way convenient. I want to find the vertices of the quadrilateral.
Image of plotted outline
So far, I've considered using linear programming to find the coordinates. However, because of the possible granularity, this wouldn't work. Also, it would probably require heuristics to implement, which I want to avoid for robustness.
Obviously, one can find two coordinates by doing the highest and lowest y-value, but from there, I'm rather stuck at where to go next.
How can I get the coordinates of the corners?
Note - I've tagged this python, as that's the language that I'm using for my project, though a description of an algorithmic approach would be a much-appreciated answer as well.
If data points have exact coordinates (exactly lie on quad sides), you can get some extremal point (topmost for example), then sort other points by angle, choose the smallest angle, аnd get the farthest point with this angle - next vertex, then repeat with new vertex and so on - it is like convex hull building with gift wrapping algorithm.
If points positions are not perfect, consider approximation of sides with straight lines using Hough transform
Some libraries like OpenCV contain Hough transform inplementations, as well as convex hull ones.
I'm trying to find the maximum projected area of distorted-3D-closed-polygon.
This one is original polygon, which is regular.
I distorted this polygon by randomly choosing two point, and bending the structure of polygon around the line that connects two points.
This is the distorted polygon.
What I want to do is calculating the projected area of this polygon.
I initially thought that this could be calculated like other self-intersecting or overlapping ones, however, it wasn't.
For example, the two different shapes above have the same topology when projected.
However, the areas that has to be concerned is different.
Therefore, it is necessary to take 3D topologies into account, and this is where I stucked.
How can I handle the complexity of this problem?
I have a set of 3d points in a txt file in the form of (x,y,z) as shown in figure 1. I want to specify the boundaries of these points as in figure 2 such that if any new points were added outside the boundaries they are deleted as the blue points, and if they are inside the boundaries as the green ones they are kept. How can I achieve that in python? I tried convex hull but it only gets the boundary points !
The real data can be found here, I used figures for simplification. https://drive.google.com/file/d/1ei9NaJHN922pYItK2CRIXyLfwqm_xgrt/view?usp=sharing
Figure 1
Figure 2
For 2D points, you can apply the test as described in Wikipedia:
One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times. If the point is on the inside of the polygon then it will intersect the edge an odd number of times. The status of a point on the edge of the polygon depends on the details of the ray intersection algorithm.
The n-dimensional case involves a convex hull test and requires linear programming techniques as described here.
I have a set of co-planar points in 3D. Some of these points that are co-planar form a circle while some co-planar points do not.
Is there a way for me to program (ideally python) and determine what points are forming a circle and what points do not?
I have seen solutions for this problem in 2D space here which probably won't work in 3D while the solutions proposed here are "mathematica" focused that I am unable to apprehend fully.
Can someone please guide me towards the possible solution?
Any three points always lie on a circle. Find one circle, then test any other points. Doing that in 3D is almost the same as in 2D (https://www.gamedev.net/forums/topic/489058-how-to-draw-a-circle-given-three-points-in-3d-space/)
I have used this python triangle module to create a mesh of triangles from a set of random 2d point coordinates. What I want now is to, without adding any points or splitting up the triangles, modify the triangle point position so that the triangles are more spaced between each other, so that they are equilateral or closer to equilateral, and more similarly sized. I honestly don't have a clue of how to do this. I would rather also have the actual code than rely on a third party library.
Look for "Laplace" or "Area centroid weighted". More details here.