Finding Records in a Radius postgresql - postgresql

I am not sure what wrong with below query:
SELECT id, earth_distance(ll_to_earth( 41.273147, -75.896858 ),
ll_to_earth(business_address_latitude, business_address_longitude)) as distance_from_current_location FROM events
WHERE earth_box( ll_to_earth(41.273147, -75.896858), 20000.00) #> ll_to_earth(business_address_latitude, business_address_longitude);
as trying to search the records in the radius of 20000 but as i checked the result it give me the records which is more then 20000 mtr distance like 24286 meter distance.
I am not sure whats wrong at the above query.
Any suggestion plz.

There is nothing wrong, you're just using the box instead of circle so it's normal. The manual states clearly:
Some points in this box are further than the specified great circle
distance from the location, so a second check using earth_distance
should be included in the query.
So just select the points that have distance <=20000 in the an outer query.

Related

How to filter points in postgres?

I have a table with points in the Postgres. The points are from the flight path. I need to filter out some points.
.
My question would be how can I select only points which are in line and then make a line from the selected points only if parallel lines are in no more then 20m distance in between lines. Turning points should be ignored.
What I have done so far is to select points that are in one line
WITH routes as (
SELECT
geom,
heading-lag(heading) over (order by time) AS direction
FROM mytable
)
SELECT direction, geom
FROM routes WHERE direction between -10 AND 10;
In my query, I calculated direction from heading, and selected points with a minor difference in the heading.
However, I don't know how to continue.
EDIT
Link to fiddler data table http://sqlfiddle.com/#!17/3262c/9/0
With my query from above, I can filter the points which are marked in red lines. How can I add those points in variables like line1=...
line2=... line3=... line4=... and line5=... ?
Thank you for any help.

How to find all points within polygon in postgis?

I have locations stored in location_table (point_location geometry), now i draw a polygon on google map and pass that polygon (geometry) to backend, I want to find all the locations that are within that polygon.
SELECT POINT_LOCATION
FROM LOCATIONS_TABLW
WHERE ST_Contains(GeomFromEWKT(?), POINT_LOCATION);
This is giving me random results when I pass the polygon from google maps to backend. Its not giving me all points that are exactly within the polygon. It gives me points that are even outside the polygon.
What is the correct way to find all points within polygon in postgis with accuracy (including border cases also)
Update :
we tried with st_intersects() it did not work as well.
UPDATE
Please find below queries
SRID=4326;POLYGON((-103.30549637500008 20.852735681153252,-103.08103481249998 20.612974162085475,-101.6261045 20.537532106266806,-99.83567868749998 20.395877027062447,-99.80306537500002 22.0572706994358,-99.64994812500004 28.918636198451633,-121.1212769375 8.69559423007209,-103.30549637500008 20.852735681153252))
SRID=4326;POINT(-103.496956 20.722446)
SRID=4326;POINT(-103.4955 20.723544)
select ST_Intersects(GeomFromEWKT('SRID=4326;POINT(-103.496956 20.722446)'), GeomFromEWKT('SRID=4326;POLYGON((-103.30549637500008 20.852735681153252,-10
3.08103481249998 20.612974162085475,-101.6261045 20.537532106266806,-99.83567868749998 20.395877027062447,-99.80306537500002 22.0572706994358,-99.64994812500004 28.918
636198451633,-121.1212769375 8.69559423007209,-103.30549637500008 20.852735681153252))'));
This Should Return False, but it's returning true.
You can use
SELECT POINT_LOCATION
FROM LOCATIONS_TABLE
WHERE ST_Contains(ST_GEOMFROMTEXT('POLYGON((P1.X P1.Y, P2.X P2.Y, ...))'), LOCATIONS_TABLE.POINT_LOCATION);
Note: Polygon must be closed (that means the last coordinate == first coordinate). Second parameter POINT_LOCATION must be the geometry column in your point table.
UPDATE:
I have tried to replay your steps in my pg database. I created 2 tables, LOCATIONS_TABLE (id, geom) and POLYGON (id, geom). After that i filled the LOCATIONS_TABLE with the 2 points
SRID=4326;POINT(-103.4955 20.723544)
SRID=4326;POINT(-103.496956 20.722446)
After that i inserted the polygon in the POLYGON table
SRID=4326;POLYGON((-103.305496375 20.8527356811533,-103.0810348125 20.6129741620855,-101.6261045 20.5375321062668,-99.8356786875 20.3958770270624,-99.803065375 22.0572706994358,-99.649948125 28.9186361984516,-121.1212769375 8.69559423007209,-103.305496375 (...)
I visualized the situation in qgis, see picture below:
As you can see, the 2 points are inside the polygon. So i manually created a point outside the polygon. After that, you can use the following sql query, to see if the points are inside the polygon:
SELECT ST_Contains(polygon.geom, point.geom)
FROM public."LOCATIONS_TABLE" point, public."POLYGON" polygon
It returns t for the 2 points inside and false for the third point.

Aggregate an Running Sum in Tableau?

I have a simple data set showing the number of points scored by a player. It is plotted across a [Week Number] x-axis. I added a Running Sum calculation, as a Line, split by Player (color).
For each week, I would like to identify the top player, by total points achieved to-date.
For example, in Week 3, the top player was GREEN with a running total of 8 points.
I tried creating a LOD function {FIXED [WEEK]: MAX(RunningSum)} but it complains that I cannot aggregate an aggregate.
I tried WINDOW_MAX(), RANK(), and tried adjusting the Table Calc, Advanced, Partitioning and Addressing feature.
Is there no way to identify the MAX of a collection of running totals, by discrete [Week Number]?
This can be done fairly easily in TSQL by wrapping a query in outer queries and applying Window functions with partitions defined.
You can absolutely just use RANK(), you just need to make sure the table calculation is being computed using Player. I imagine your [RunningSum] field looks something like this:
RUNNING_SUM(SUM([Points]))
Create a table calculation (let's call it [Player Rank]):
RANK([RunningSum])
Then we need to edit the table calculation. You can do this directly from the Calculated Field edit window (there's a blue link above the OK and Apply buttons that says something like "Default Table Calculation").
We want to compute the rank of our players, so we'll go into "Compute Using:" and select Player.
For a quick sanity check, drag [Player Rank] into Tooltip and check it out.

Put points on the map, longitude/latitude not where they should be although SRID the same

Thank you for looking at this - I'm sure it's a simple question but this is all very new to me and after a lot of internet searching my points are still in the sea!
I have a set of UK postcode data that I have converted into latitude and longitude. I also have a UK counties shape file.
The code that I have used to convert the latitude/longitude to a geometry point column is as follows:
ALTER TABLE colic ADD COLUMN longlat geometry(POINT,27700);
update colic set longlat = st_setsrid(st_point( longitude, latitude), 27700);
And this produces a column that has values as follows (I've only included a couple!):
"0101000020346C000048BF7D1D3867E83FC05B2041F1334A40"
"0101000020346C0000F085C954C1A8F7BFA1F831E6AE954A40"
"0101000020346C000020D26F5F07CEF4BFE3361AC05B504A40"
"0101000020346C00007D3F355EBA49F73FB6847CD0B3614A40"
The .prj from my shape file is:
PROJCS["British_National_Grid", GEOGCS["GCS_OSGB_1936",
DATUM["D_OSGB_1936", SPHEROID["Airy_1830",6377563.396,299.3249646]],
PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",400000.0],
PARAMETER["False_Northing",-100000.0],
PARAMETER["Central_Meridian",-2.0],
PARAMETER["Scale_Factor",0.9996012717],
PARAMETER["Latitude_Of_Origin",49.0], UNIT["Meter",1.0]]
And I set its SRID to 27700 when I upload it using the PostGIS shapefile uploader.
In PG Admin, when I ask:
select st_srid(geom) from counties limit 1;
select st_srid(longlat) from colic limit 1;
I get 27700 for both.
But...when I try any spatial query I get no response, and when I load the layers into quantum, the point data are off the tip of Cornwall in the north Atlantic, and only represented as one dot on the scale of the counties map.If I zoom in on them, they are 'there', but just not in east anglia where they should be!
This is my first attempt at both SQL and mapping - I'm sure there is something really simple that I have missed.
You are mixing up spatial references (SRS). The SRS in the .prj file (aka SRID=27700) is projected eastings and northings, which have units of metres. This is not latitude and longitude! Furthermore, it is a deception to call a column longlat when it isn't for longitude/latitude coordinates.
If you try to insert latitude/longitude in a column with SRID=27700, the points will not behave as expected, e.g., they will be in the far bottom left corner of a map.
If you have lat/long data from WGS84 (SRID=4326), you can transform this to eastings and northings:
UPDATE colic SET
geom = ST_Transform(ST_SetSRID(ST_Point(longitude, latitude), 4326), 27700);

Postgis: How to build query which returns points within box initially and then all the other points?

There is code which get from database points in bounding box for rendering on browser. Also displays list of points with attributes (name etc.)
But, when filter will be applied to the dataset I may get big count points (example around 50000). Rendering 50000 points by browser may cause performance problem. Therefore in my opinion need to apply paging algorithm (by LIMIT and OFFSET). But, first need render filtered points within browser box and then all the other.
UPD:
I found this variant:
SELECT gs.id, gs.name, ST_Contains(ST_GeomFromText('POLYGON(...)', 4326), gs.point) as contains
FROM geoms as gs
WHERE gs.name LIKE '%Berlin%'
ORDER BY contains DESC
LIMIT 50
The query you have will work for what you need but are you sure you need to display 50k points most of which are outside of the window? The standard aproach would be querying the points inside the browser box and a little around it and then load the rest when the position changes.