Postgis ST_Intersects for circle giving wrong values - google-maps-api-3

I need to return true when a particular point(lat/longs) is present inside my geofence circle. But the query is retuning true even though the point is outside my geofence circle.
Please find the query below.
select ST_Intersects(ST_Buffer(geofence_polygon, 127.08), ST_POINT(18.595798 ,73.78833)) from masterdata.al_m_geofence
In this query, geofence_polygon is of type geography.
127.08 - radius of circle in meters
18.595798 - latitude
73.78833 - longitude
The query should return true only when the point is inside the circle.
Please let me know, whether this query is correct or not.

Your ST_POINT arguments are backwards:
geometry ST_Point(float x_lon, float y_lat);
Should be:
ST_POINT(73.78833,18.595798)
full query:
select ST_Intersects(ST_Buffer(geofence_polygon, 127.08), ST_POINT(73.78833,18.595798)) from masterdata.al_m_geofence

Related

What's the unit for radius in GeoFire?

I'm creating a query using GeoFire, which accepts two parameters; center (a 1D array containing the values of latitude and longitude for the centre of the geospatial query), and radius, which in their documentation is expected to be set as a scalar double value i.e. 10.5.
What is the unit of the radius? I imagine it is likely km, but I haven't found any documentation to support this.
The unit for radius in a GeoFire query is Kilometers
https://github.com/firebase/geofire-js/blob/master/docs/reference.md#geofirequeryquerycriteria

Find all lat/long under certain radius

I have set of lat/long which are stored in database and I have to find all the lat/long from the database which are lies between the given lat/long(e.glatitude = 37.4224764,and longitude=-122.0842499) and given radius(say 5miles).For this I need to do some addition and subtraction like:
37.4224764 + 5mile
37.4224764 - 5mile
and
-122.0842499+5mile
-122.0842499-5mile
But I don't know how the conversion works here.
You're a little vague in your requirements, do you just want to find out the coordinates of a point a certain distance from your current location in a given direction? Or get a circle of a radius of a particular distance around your coordinates? Or draw a line of a certain length from your coordinates to another point a particular distance away?
I assume you'd probabaly want to use the Geometry spherical library, e.g.
var latLng = new google.maps.LatLng(geometry.location.lat, geometry.location.lng);
var newLatLng = google.maps.geometry.spherical.computeOffset(
latLng,
1000,
0
);
see also https://developers.google.com/maps/documentation/javascript/geometry#Distance
PS: you need to add the geometry library parameter when loading the map:
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>

Point In Polygon using T-Sql

I am creating a mapping application which users can interactively draw a polygon on a Google Map.
I need to send this polygon data back to a Sql Server and determine which records to return based on their LatLng positions in the database being within the polygon.
Does anyone know how to do this or a routine that is available.
Thanks in advance
If you have a geography column associated with your records, you can use that to query for items within a polygon. Link to get you started. Here's an example:
--What Items are within the Bermuda Triangle?
---Note that Well-Known Text representation of a polygon is composed of points defined by Longitude then Latitude.
---Note that the points are defined in a counter-clockwise order so that the polygon is the interior of the triangle.
DECLARE #BermudaTriangle geography = geography::STPolyFromText
('POLYGON((
-80.190262 25.774252,
-66.118292 18.466465,
-64.75737 32.321384,
-80.190262 25.774252
))', 4326)
SELECT ItemID, ReportedGPSPoint.Lat, ReportedGPSPoint.Long
FROM Items
WHERE ReportedGPSPoint.STIntersects(#BermudaTriangle) = 1

Determine if a point is on a vertices of a polygon

Given the coordinates of a polygon,
I found many algorithms to detect if a point is in a polygon - for example: [1] and [2]. But none of these algorithms are able to detect if the point lies on the vertices of this polygon. For example I have a polygon:
|---------------------|
| |
| |
|---------------------|
My point is in the right upper corner. I want an algorithm that tells me whether the point is inside the polygon. How can I do this?
Except for problem of dealing with issues related to floating point not exact match but close enough, the same algorithm should work for both.
Just pick a point inside the polygon, create the test line segment from the test point to the inside point, and then, for each segment in the polygon, determine if a test line segment intersects that polygon segment, To count as intersect, count intersects open on one end of polygon segment, and closed on the other end, i.e., if the intersection is exactly the same as the start of the polygon, count it, but if it's exactly the same as the end point, do Not count it. You need to do this so that when the test segment intersects with a polygon vertex, you must only count it as intersecting one of the two segments on either side of the vertex.
Then If test point is inside polygon, the number of intersections will be an even number, if it is outside, it will be an odd number.
Just found a solution right here. It is very easy and the code from [1] has a and b already implemented. Additional information to the source code can be found here.
const float EPSILON = 0.001f;
bool IsPointOnLine(Point linePointA, Point linePointB, Point point)
{
float a = (linePointB.y - linePointA.y) / (linePointB.x - linePointB.x);
float b = linePointA.y - a * linePointA.x;
if ( fabs(point.y - (a*point.x+b)) < EPSILON)
{
return true;
}
return false;
}

All latitude and longitude within 1 mile circle area

I am trying to generate pins over the map of my database coordinates by using fusion table and I want to generate only within 1 mile area circle around the specific location. To get 1 mile area's coordinates similar to my db coordinates I've to put condition of < and > to fusion table but I don't know how to get all the coordinates within 1 mile area circle or what should be the value for condition in query. Can any one please help me to figure out, it'll really appreciable.
You haven't provided any code, so I'm not exactly sure what you want to do. But the Fusion Table ST_INTERSECTS operator should do what you want. Look for <spatial_condition>.
var where = "ST_INTERSECTS('location_col',CIRCLE(LATLNG(31.954109,-115.441528), radius_in_meters) ) )";
var qryOpts = {
query: {
select: location_col,
from: table_id,
where: where
}
};
ft_layer.setOptions(qryOpts);
I have no idea if that is possible within the Fusion Tables API but, if I were doing it, I would first calculate which points in my database were within the radius and insert only those. You can ignore the earth's curvature and just use the Pythagorean theorem with a little adjusting for your latitude.
See here: http://www.movable-type.co.uk/scripts/latlong.html

Resources