How to find maximum radius? - logic

How to find max radius of earth at a given longitude and latitude?
Max radius_ Limit to events within the specified maximum number of kilometers from the geograhic point defined by the latitude and longitude parameters

Related

Heading angle for waypoint navigation (fails at bounds)

In a Cartesian coordinate system / Euclidian plane, a vehicle travels clockwise around the origin with radius 1 and currently at angle θ = PI/4 (45°), so its heading is 7/4*PI (315°).
To reach its new destination (the origin) it should change course (instantly) to -5/4*PI (-135°).
This angle can be obtained with e.g. atan2 on the components of the vector from position to target (with the target at origin, it's the the inverted position vector)b:
Finding Signed Angle Between Vectors
Using atan2 to find angle between two vectors
Algorithm Improvement: Calculate signed change in heading
However, due to inertia, the vehicle cannot change its course instantly and crosses the X-axis (x=1, y=0, θ=0), where a new calculation results in a course of PI (180°).
The vehicle tries to achieve this new (positive angle) course by a positive turn (counterclockwise, CCW), which takes it back across the X-axis, and so on (it "wiggles" away from the target along the x-axis).
It breaks also if negative angles are "wrapped" into the absolute positive range 0...2PI - example data of the southbound vehicle passing west of the target:
course -0.00477
adjust 6.27840
course 0.00034
adjust 0.00034
The proportional control then basically causes a negative (clockwise) turn, which is the wrong direction.
Do I eliminate this "overflow" somehow (not limit to 0...2PI but map absolute angles to the vehicle's) or should I use a different strategy altogether (which)? 😕
Not limiting the vehicle angle to 0...2PI, i.e. allowing negative and large angles seems to be a promising approach.
E.g. going from 10° to 350°=-10° means a -20° turn.
This determines the heading with the smallest turn angle:
delta = course - heading%fullcircle
if (delta > halfcircle)
delta = delta - fullcircle
if (delta < -halfcircle)
delta = delta + fullcircle
newheading = heading + delta
halfcircle and fullcircle correspond to 180°/PI and 360°/2*PI respectively.
The vehicles heading will "wind up" if it's going circles, but if need be, this could be restored by an occasional modulo operation.

Find points in four directions of a latitude and longitude at certain distance

I am working on showing four pointers around a location (a pointer with certain latitude and longitude), which can be selected on a map.
So i get the location's lat and long values from https://nominatim.org/ api. What i'm trying to work on is get four point's lat and long value from the selected location. These four points are simply 100 meters in distance from the current point.
So now the issue here is how do i calculate or find a point east, west, north and south of the selected location's point (100 meters from the current lat long position). i have checked different options like point in polygon algorithm here https://assemblysys.com/php-point-in-polygon-algorithm/, or trying to draw a circle around the selected point and then getting four points on that circle's circumference, however the issue is still that i'm not able to know if the point which i have got is in which direct from the selected location. Is it east of location, south or west etc.
Any help is appreciated about identifying that a certain point is in which direction of the selected point.
Once you (or someone) select the location on the map, you get its latitude and longitude. Let's say it is:
lat = 44.78
lon = 20.45
Then, take a look at this answer: Calculating new longitude, latitude from old + n meters.
It should give you the distance of 100 meters, but as latitude/longitude offset. Once you apply the formula from SO answer, you would get:
lat_offset = 0.0008983152841195215
lon_offset = 0.001265559599380766 // note that this lon_offset contains lat in formula (take a look at the link above)
Finally, apply the calculated in your formula:
west_lat = lat
west_lon = lon - lon_offset
east_lat = lat
east_lon = lon + lon_offset
north_lat = lat + lat_offset
north_lon = lon
south_lat = lat - lat_offset
south_lon = lon
Note: These formulas does not work for positions that are very close to south or north pole

How to calculate x kilometers from a lat/lon

I have a lat/lon and I'd like to calculate X kilometers North South East and West from that location. How would I go about doing that?
I read that "The number of kilometers per degree of longitude is approximately"
(2*pi/360) * r_earth * cos(theta)
Where theta is the latitude in degrees and r_earth is approximately 6378 km.
I'm not sure, for instance, how to calculate 3km North of a 37.875942,-122.3156416

Given a datetime, calculate the lat/lon coordinates where the sun is directly overhead

Some variants of this question have been asked before, but not this exact one. So here goes:
Given a particular point in time, how do I calculate the lat/lon coordinates of a point on the surface of the Earth where the Sun is directly overhead?
I can get the declination and the right ascension, and those numbers seem accurate. It should be a piece of cake from here but it's getting late and I'm completely lost.
Any help?
Since you are assuming earth to be a sphere, you can assume latitude to be a sinusoidal function of day of the year (more precisely cosine. Normalize 365 days to 2pi and when day = june 21st, the value is equal to 0).
Longitude will depend on the time of the day. Normalize time so that one day = 360 degrees and offset accordingly.
Details:
AT present tropic of cancer is at latitude L = 23° 26′ 16″
So, Latitude = L*cos( (X-a)/b ), where a=June21st, b = 365.25/2pi .
Longitude = (time - t0 ) *360 /24, where time is current time in hours(UTC), t0 is the offset.

Algorithm to find all Latitude Longitude locations within a certain distance from a given Lat Lng location

Given a database of places with Latitude + Longitude locations, such as 40.8120390, -73.4889650, how would I find all locations within a given distance of a specific location?
It doesn't seem very efficient to select all locations from the DB and then go through them one by one, getting the distance from the starting location to see if they are within the specified distance. Is there a good way to narrow down the initially selected locations from the DB? Once I have (or don't?) a narrowed down set of locations, do I still go through them one by one to check the distance, or is there a better way?
The language I do this in doesn't really matter. Thanks!
Start by Comparing the distance between latitudes. Each degree of latitude is approximately 69 miles (111 kilometers) apart. The range varies (due to the earth's slightly ellipsoid shape) from 68.703 miles (110.567 km) at the equator to 69.407 (111.699 km) at the poles. The distance between two locations will be equal or larger than the distance between their latitudes.
Note that this is not true for longitudes - the length of each degree of longitude is dependent on the latitude. However, if your data is bounded to some area (a single country for example) - you can calculate a minimal and maximal bounds for the longitudes as well.
Continue will a low-accuracy, fast distance calculation that assumes spherical earth:
The great circle distance d between two points with coordinates {lat1,lon1} and {lat2,lon2} is given by:
d = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
A mathematically equivalent formula, which is less subject to rounding error for short distances is:
d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
d is the distance in radians
distance_km ≈ radius_km * distance_radians ≈ 6371 * d
(6371 km is the average radius of the earth)
This method computational requirements are mimimal. However the result is very accurate for small distances.
Then, if it is in a given distance, more or less, use a more accurate method.
GeographicLib is the most accurate implementation I know, though Vincenty inverse formula may be used as well.
If you are using an RDBMS, set the latitude as the primary key and the longitude as a secondary key. Query for a latitude range, or for a latitude/longitude range, as described above, then calculate the exact distances for the result set.
Note that modern versions of all major RDBMSs support geographical data-types and queries natively.
Based on the current user's latitude, longitude and the distance you wants to find,the sql query is given below.
SELECT * FROM(
SELECT *,(((acos(sin((#latitude*pi()/180)) * sin((Latitude*pi()/180))+cos((#latitude*pi()/180)) * cos((Latitude*pi()/180)) * cos(((#longitude - Longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM Distances) t
WHERE distance <= #distance
#latitude and #longitude are the latitude and longitude of the point.
Latitude and longitude are the columns of distances table. Value of pi is 22/7
Tank´s Yogihosting
I have in my database one goups of tables from Open Streep Maps and I tested successful.
Distance work fine in meters.
SET #orig_lat=-8.116137;
SET #orig_lon=-34.897488;
SET #dist=1000;
SELECT *,(((acos(sin((#orig_lat*pi()/180)) * sin((dest.latitude*pi()/180))+cos((#orig_lat*pi()/180))*cos((dest.latitude*pi()/180))*cos(((#orig_lon-dest.longitude)*pi()/180))))*180/pi())*60*1.1515*1609.344) as distance FROM nodes AS dest HAVING distance < #dist ORDER BY distance ASC LIMIT 100;
PostgreSQL GIS extensions might be helpful - as in, it may already implement much of the functionality you are thinking of implementing.
As biziclop mentioned, some sort of metric space tree would probably be your best option. I have experience using kd-trees and quad trees to do these sorts of range queries and they're amazingly fast; they're also not that hard to write. I'd suggest looking into one of these structures, as they also let you answer other interesting questions like "what's the closest point in my data set to this other point?"
What you need is spatial search. You can use Solr Spatial search. It also got lat/long datatype built in, check here.
You may convert latitude-longitude to UTM format which is metric format that may help you to calculate distances. Then you can easily decide if point falls into specific location.
Since you say that any language is acceptable, the natural choice is PostGIS:
SELECT * FROM places
WHERE ST_DistanceSpheroid(geom, $location, $spheroid) < $max_metres;
If you want to use WGS datum, you should set $spheroid to 'SPHEROID["WGS 84",6378137,298.257223563]'
Assuming that you have indexed places by the geom column, this should be reasonably efficient.
Thanks to the solution provided by #yogihosting I was able to achieve similar result from schemaless columns of mysql with codes shown below:
// #params - will be bound to named query parameters
$criteria = [];
$criteria['latitude'] = '9.0285183';
$criteria['longitude'] = '7.4869546';
$criteria['distance'] = 500;
$criteria['skill'] = 'software developer';
// Get doctrine connection
$conn = $this->getEntityManager()->getConnection();
$sql = '
SELECT DISTINCT m.uuid AS phone, (((acos(sin((:latitude*pi()/180)) * sin((JSON_EXTRACT(m.location, "$.latitude")*pi()/180))+cos((:latitude*pi()/180)) *
cos((JSON_EXTRACT(m.location, "$.latitude")*pi()/180)) *
cos(((:longitude - JSON_EXTRACT(m.location, "$.longitude"))*pi()/180))))*180/pi())*60*1.1515*1.609344) AS distance FROM member_profile AS m
INNER JOIN member_card_subscription mcs ON mcs.primary_identity = m.uuid
WHERE mcs.end > now() AND JSON_SEARCH(m.skill_logic, "one", :skill) IS NOT NULL AND (((acos(sin((:latitude*pi()/180)) * sin((JSON_EXTRACT(m.location, "$.latitude")*pi()/180))+cos((:latitude*pi()/180)) *
cos((JSON_EXTRACT(m.location, "$.latitude")*pi()/180)) *
cos(((:longitude - JSON_EXTRACT(m.location, "$.longitude"))*pi()/180))))*180/pi())*60*1.1515*1.609344) <= :distance ORDER BY distance
';
$stmt = $conn->prepare($sql);
$stmt->execute(['latitude'=>$criteria['latitude'], 'longitude'=>$criteria['longitude'], 'skill'=>$criteria['skill'], 'distance'=>$criteria['distance']]);
var_dump($stmt->fetchAll());
Please note the above code snippet is using doctrine DB connection and PHP
you may check this equation
i think it will help
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

Resources