I know that most people will view this question and point me to Google Geocode - but I'm looking for a mathematical formula that allows someone to take a Lat/Lng point and see if its inside a US state (or a bounding box). Is there a way via PHP, that I can do a calculation to see if a point is in a certain Box (such as California)?
Well, there's no formula that'll tell you anything about what states is where (it would have totally been a spoiler as to the outcome of the US-Mexico war if there was!) So you'll need to get that data from somewhere.
This then turns into one of two problems, depending on the degree of accuracy you want.
If you have details of a bounding box that is rectangular when shown on a Mercator or similar projection (that is, it has degrees of latitude for north and south, and of longitude for east and west), then the formula is simply:
inBox = latitude <= north && latitude >= south && longitude <= west && longitude >= east
If you have more detail, and have a series of points that defines the border of the state (obviously, the more points, the more precision) then it becomes a variant of the point-in-polygon problem, with a guarantee of only involving simple polygons (no US state has a border that crosses itself, nor completely surrounds that used in this C code. It's possible that there would be edge cases affected by the fact that this is a 2D-plane algorithm rather than a spherical one, but I imagine you'd need to have some pretty precise data on the boundaries of the states for the imprecision from the algorithm to be greater than that caused by the data.
The simplest way I would think is using bound box for each state, that can be found from Flicker Geo API, an example for CA- https://www.flickr.com/places/info/2347563
Related
Here's an example of what I want to figure out:
Device A is at 40.7128 / -74.0060 (lat/lon)
Location B is at 40.730610 / -73.935242
Radius = 10 miles
And the question:
Is Device A within the radius around Location B?
I don't care about the language or technology, just want to get the job done. I know some Python, JS, and Java.
Anyone know a good/efficient way to approach this?
I would calculate the distance between the two points and check if it's inferior to the radius.
This thread gives some ideas on how to implement it in python: Getting distance between two points based on latitude/longitude
I have a collection of Points (lat, long for a collection of buildings) and I want to group them based on whether they are within x meters of each other. I know that I could do this pairwise by first using Geopandas buffer() function (with x/2 meter radius) and then using sjoin(). However, I don't want to just do this pairwise. I want to group all buildings whose buffer region (the lat, long as the center and the buffer being a circle of radius x/2 meters) overlaps with ANY OTHER buffer region.
For example, if I have three buildings (denoted A, B and C), with each building 25 meters from its neighbor and I use a 25 meter buffer, then A and B can be grouped with sjoin() and B and C can be grouped, but I would want all THREE to be grouped.
That's in contrast to the case where A and B are 25 meters apart and C is 50 meters from B. In that case, I would want to be able to group A and B together and C in its own group.
In reality, I have potentially 100 or more buildings, so it isn't possible to run all permutations pairwise. I would need a function that groups multiple buildings whenever the building's buffer circle intersects with any other buffer circle.
Is there a simple way to do this with Geopandas?
Thank you for your responses. I wound up doing the following:
buffered each building's lat/long Point by a set distance, in meters (** see note below **)
determined the geopandas.unary_union of all of the buffers to get a multi-polygon (list of polygons)
wrote a custom function to identify which polygon in the list intersects with a specific buildings Point (using geopandas's .intersection), and called that list index the "location group"
used panda's apply with the above function to get the "location group" that each building belonged to
used panda's groupby to sum the values based on the "location group"
For the above, I had to be a bit careful about edge cases where the intersection was not a multipolygon but rather a polygon (or even a Point, in cases where the buffer was zero).
A bigger issue I ran into was enormous trouble getting geopanda's buffer function to work properly. I know that this has to do with the crs/projections and tried all sorts of variations, but could never get the buffer function to work properly with the buffer distance in meters. I found a nice custom function here (Is there an easy way to create square buffers around point and if they intersect, merge them?) that did work, but then realized that my Postgres setup has the PostGIS functions and used ST_Buffer to get the buffer polygons at the same time I am querying the database for the building lat/long data.
Hopefully the above will be helpful to others.
I need to calculate the minimum distance between a 3D point (latitude, longitude, elevation) and a line (defined as two points).
The elevation is not necessary on the ground, I need to consider flying objects.
I only found an article that explains how to do that on a generic space but my points are defined with lat/lon/altitude(meters).
Thank you for pointing in the right direction, in my case I need to do that in Javascript but couldn't find any library that takes into consideration the altitude.
Point-Line Distance--3-Dimensional
If you want to compare a 3d point to a 2d line, I suppose you mean a "line" on our earth, at elevation 0. Take a look at st_distance in postgis.
If I understand you correctly, that'll give you what you want.
https://postgis.net/docs/ST_Distance.html
Given an MKMapView that contains a variable amount of annotations ([mapView annotations]) at various points on the map and the MKMapRect value MKMapRectWorld, how can I determine an area on the map that has the greatest concentration on MKAnnotation objects (perhaps the 5-15 annotations closest to one another) ?
Example scenarios:
* Coffee finder: Determine which area of the map has the most Starbucks
* K9 statistics: Determine which area of the map has the most Cocker Spaniels
The "area" could be a set rect size or determined by a block of annotations, I don't necessarily care. Thanks for your help!
You will find related question helpful.
Also take look at K-means_algorithm
K-means_algorithm
If you have N annotations and want to break into K parts you can find center (which will fill certain criteria. e.g. minimize the within-cluster sum of squares ) of each of K parts with K-means-algorithm. Once you have center find out distance between center and annotation farthest from center it will give radius of region you are interested. There are several variations of K-means_algorithm, you can choose whichever based on performance and ease of implementation.
EDIT:
I have not implemented following, but think will definitely give one of solution
If you are OK with range 5-10, there can be multiple solutions. So we will find one of solution.
1- Say you have (N=100) annotations and want which (P =15) of them are most located densely.
2- Then we will divide N annotations in K = N/P groups(here K = 7) randomly
3- Use K-means algorithm so that finally we will have K groups that can be differentiated as separate entities.
4- These K groups will have property of minimum " within-cluster sum of squares".
5- If you want to save computational time, you can relax definition of most concentrated group as minimum "within-cluster sum of squares" as opposed to area bounded by them.
6- select a group from obtained K group that satisfies your criteria.
7- If want to persist to minimum area(greatest concentration) definition then you will need to do lots of computation
a. First determine boundary annotations of given group, which itself a huge problem.
b. calculate are of each polygon and see which is least. not complicated but computationally demanding)
EDIT2:
I tried whatever I can and finally thought it this question belongs to specialist math website. I asked your question here and from the answer , you can get paper discussing this problem and solution here. Problem they discuss is given N points, find the K points whose area of convex hull is minimum.
I'm monitoring a GPS unit which is on it's way from Cape Discovery in Canada, to the North Pole. I need to keep track of the distance travelled and distance remaining each day, so I'm using the Haversine Formula, which I'm told is very accurate for smaller distances.
I'm really bad a Math, but I have a sneaking suspicion that the accuracy depends greatly on the radius of the Earth, and since the universe decided to make Earth out of an oblate spheroid, I have a choice of approximations for Earths radius to choose from.
Since I'm monitoring coordinates very close to the north pole, I'm wondering wether anyone knows which radius is going to provide the most accuracy.
Mean Equatorial: 6,378.1370km
Mean Polar: 6,356.7523
Authalic/Volumetric: 6,371km
Meridional: 6367km
Or any other kind of Radius that anyone knows about?
I'm hoping some maths or cartography whizz might know the answer to this one.
You could approximate the actual radius at the point(s) where you're measuring the distance (provided that you calculate a sequence of relative small distances).
Assuming the earth being an ellipsoid with the main axis a being the mean equatorial radius and the second axis b being the mean polar radius, you can calculate the point on the ellipse represented by these two axes by using the current latidude. The calculation is shown and explained here.
(Note: This ellipse can be thought as a cross section of the earth through the poles and the point where you want to calculate the distance)
This gives you a point q=(qx,qy), the radius at this point being r=sqrt(qx^2+qy^2). That's what I'd use for calculating the Haversine formula.
It doesn't really matter - they are all going to be wrong if you just treat the earth as a sphere. I would probably use the polar since you are mostly going north.