python: elegant way to find the locations inside the radius of provided location using Latitude and Longitude - python

I have a set of coordinates (Longitude and Latitude) in decimal notation, and I'm looking for a way to find the coordinates in a circle with variable radius around each location.
Here in above figure you shown clearly red location, first we want do draw a circle around the center point (red location) here we only know the latitude and longitude of that point so my question is, how to draw circle if we only know the latitude and longitude. (in python without using any module or library )
Second if draw the circle then how to get all the location that are inside the circle.
Please help me to achieve these two points in Python without any use of library or any other module.

You ask a question for drawing etc but you never mentioned on what you plotting on.
It sounds a pretty good usecase for matplotlib.basemap(have done a pretty big project embeded in PyQt5).Starting from what you using to visuallize ,plotting a circle is really easy.Also for your second question finding all points in a circle can be infinite depending on what your coordinates decimal number is , i think you just want to check after if a point belongs in this circle witch is fairly easy too.
I suggest you take a look to basemap or cartropy libraries.
Have fun.

Related

How to check if a 2D point is inside or outside a 2D Closed Bezier Curve using Python?

Hello,
in my 2d software i have two inputs available:
an array of XY points
[(x,y),(1,1),(2,2),(2,3),(-1,3),...]
and another matrix representing the closed 2D bezier curve handles
[((x,y),(x,y),(x,y)),
((-1,-1),(1,1),(1,2)),
((1,1),(2,2),(2,3)),
...]
How can i check if a point is inside or outside the given curve using python ? using preferably numpy maybe
I don't know how the theory of Bezier curves, so if your second list of points is a kind of compressed way to represent a Bezier curve, first try to sample some points of the curve with the precision you want.
So you have n points of your curve, and then you can apply a simple PIP algorithm : https://en.wikipedia.org/wiki/Point_in_polygon
I can explain in details later if you want to know how to do it programmatically.
I cant write code right here, because I need the entire program to understand properly, however I may provide two approaches how to do that.
The hardest way is to approximate each Bézier curve by a polyline. And then, according to the wiki you can use two techniques:
Ray casting algorithm: the shorthand of the algorithm: You put a ray, which starting from a point and goes through the entire polygon to an another point. Some lines lies inside a polygon, some outside. And then you check to which line belongs a specific point Looks like this:
Winding number algorithm: A little bit about winding numbers. So if a winding number is non-zero, the point lies inside the polygon
The huge drawback of this approach is that the accuracy depends on how close you approximated a curve to a polyline.
The second way is to use a bitmap. For example, you set your points to the white then render the area under the curve to the black and see if your points remain white. This method is more accurate and the fastest one, because you can use the GPU for the render.
And some links related to the first a approach:
https://pomax.github.io/bezierinfo/#intersections
http://web.mit.edu/hyperbook/Patrikalakis-Maekawa-Cho/node80.html

Python package/function to get percentage area covered by one polygon in another polygon using geo coordinates

I am looking for a solution to find the percentage area covered by a polygon inside another polygon, from geo coordinates using python.
The polygon can be either fully reside inside the other one or a portion of the second polygon.
Is there a solution to this.
Please advice.
Percentage is just area of intersection over area of the (other) polygon:
area(intersection)/area(polygon2).
Basically any of geometry packages should be able to compute this, as they all support area and intersection functions: I think Geopandas, SymPy, Shapely (and others I missed) should be able to do this. There might be differences in supported formats.
You did not specify what Geo coordinates you use though. I think Geopandas and SymPy support only 2D maps (flat map) - meaning you need to use appropriate projection to get exact result, and Shapely works with spherical Earth model.

Modify polygons so that they don't overlap and area stays the same

I have a set of polygons and they can overlap with each other, like this:
I want to modify them in such a way that they don't overlap and the resulting surface area stays the same. Something like this:
It is okay if the shape or the position changes. The main thing is that they should not overlap with each other and the area should not change much (I know the area changed a little in the second image but I drew it manually thus let's just assume that the areas did not change).
I am trying to do it programmatically with the help of Python. Basically I stored polygons in a PostGIS database and with the help of a script I want to retrieve them and modify them.
I am very new to GIS and thus this seems like a difficult task.
What is the correct way of doing it? Is there an algorithm that solves this kind of problems?
Take a look at ST_buffer and try passing a signed float as the second argument (degrees to reduce radius by)
SELECT buffer(the_geom,-0.01) as geom
Be careful with negative buffers as you could run into issues if the buffer size exceeds the radius, see here.
Here is what I did:
Iterated over all the polygons and found overlapping polygons. Then, I moved the polygon in different directions and found the best moving direction by calculating the minimum resulting overlapping area. Then I simply moved the polygon in that best direction until there is no overlapping area.

Plotting a point cloud and moving the camera

I have a list of points given by their x, y, z coordinates. I would like to plot these points on a computer. I have managed to do this with gnuplot and with the python library matplotlib separately. However for these two solutions, it seems hard to change the 'viewing point', or the point from which the projection of the 3D point cloud to the 2D screen is done.
1) Is there any easy way to, preferably continuously, move the viewing point in gnuplot (the splot command) or with matplotlib (the plot command)?
2) What other libraries are there for which this is an easy task?
EDIT: I want to move the viewing point (like the player in an first-person shooter, say), not change the viewing angles.
When you change the viewing angles it's like moving your head on an equidistant shell seeing continuously the center of the image/screen/object.
What you said is moving your head in the x-y plane looking fixedly ahead so the content (the screen/object) shifts out from your sight. It is easy in Gnuplot with set origin like
set origin -0.25,0
splot exp(-x**2)*exp(-y**2)*sin(x)**2 with pm3d notitle
simply shifts the camera.
What you have mean (I think) is step left (or right or jump or...) with your player but this changes not just the viewing point but also the viewed content. You can achieve it with xrange and yrange:
splot [-5:15] exp(-x**2)*exp(-y**2)*sin(x)**2 with pm3d notitle
Of course the changing of the x/y ranges depends on the current viewing angles what you have to calculate outside of Gnuplot.
No, gnuplot cannot really move the viewing point, for the good reason that the viewing point is at infinity: all you can do is set an angle and magnification (using set view) and an offset within the viewing window (with set origin). That means, you can move the viewing point on a sphere at infinity, but not among the points you're plotting.
(Question 2 is off-topic as a software advice, but you're looking for a rendering software such as paraview)

OpenCV Python find contour point closest to a given point

In OpenCV, for a given point (x,y), what is the best way to find the point closest to it that belongs to a known contour cnt? (I assume the point lies outside the contour.)
dist = cv2.pointPolygonTest(cnt,(x,y),True)
pointPolygonTest returns the distance of the closest contour point but I do not see a way to get to the actual point.
Of course, I could loop over the list of contour points and recalculate the distance finding the one with minimum distance. (A couple of questions on SO explain more sophisticated ways for finding the closest point out of a list of points to a given point.)
Alternatively, I could draw a circle with radius dist and see where the circle and the contour touch.
Both options seem a little clunky, so I wonder if I am missing something more straight forward.

Categories