Spatialite find the nearest node for the given lat/lon - mysql

I am struggle on this for a few days, I am not sure why the query was wrong..
I want to find the nearest point at the given lat/lon.
SELECT rid,DISTANCE(geometry, MakePoint(-79.91759, 43.266571))
FROM room2f ORDER BY DISTANCE(Startpoint(geometry), MakePoint(-79.91759, 43.266571))limit 1
It's always return the first row, nomatter what point I use. Then I tried to remove "limit1":
SELECT rid,DISTANCE(geometry, MakePoint(-79.91759, 43.266571))
FROM room2f ORDER BY DISTANCE(Startpoint(geometry), MakePoint(-79.91759, 43.266571))
The result is very strange... The result is sorted by rid rather than distance, I think this is why it always return the first row. Is my query wrong? Or, there is a bug in Spatialite?
Thanks]1

In your sql statement you compare to different distances..
From your geometry in general to your specific point (as I know it is the shortest distance from the point to your geometry).
From the first point of your geometry to your specific point.
Therefore you also get a different ordering of your result!
See the reference to the distance function:
http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.0.html#p13

Related

MySQL - Find Closest Match in range of numbers

I'm working on a graph type application and I'm looking to find the best solution within a x,y axis start to end point.
MySQL Data:
Say I have the following:
starting_x: 200
starting_y: 150
ending_x: 500
ending_y: 605
So I'm wanting to find the closest match between the above numbers to the database.
My query I'm working with now:
SELECT * FROM `graph` ORDER BY `start_pos_x`,`start_pos_y`,`end_pos_x`,`end_pos_y` ASC
I know this is not even close to what I'm trying to do but i'm having a hard time finding a solution on here.
Thanks!
This seems to be an algorithm question.
First, you must define the closest match between the above numbers to the database. For example, I can define it as to find "the lowest sum of the squares of the differences between Xs and of Ys."
Then, the solution might be something like:
Select
*
From
`graph`
Order by
Power(starting_x-200, 2) + Power(starting_y-150, 2)
+ Power(ending_x-500, 2) + Power(ending_y-605, 2) ASC

Query with negative numbers not working as expected

I'm trying to write a query to this data set:
https://data.sfgov.org/City-Infrastructure/Street-Tree-List/tkzw-k3nq
I want to return the records that have a latitude AND longitude between certain values.
My attempt at a query string:
"https://data.sfgov.org/resource/2zah-tuvt.json?$limit=1000&$where=latitude between 37.709864 and 37.781918 AND longitude between -122.398942 and -122.501212"
The request limits the response to 1000 records and searches for records with a latitude between the specified numbers AND a longitude between the specified numbers.
The request does not produce an error but it also doesn't include any results. There are thousands of records that should satisfy the parameters and I'm not sure why I'm not getting a response.
When I test off of the latitude values only, the response returns as expected. When I test off of the longitude values only, I get no response. I've tested the longitude for values < 0 (since all of the longitude values are negative numbers) and that does produce a correct response.
I have a feeling the negative numbers I'm trying to search for are causing the issue. Is there some way to format the negative numbers so SoQL sees them as part of the search number instead of an operator? I've also tried wrapping the negative numbers in parentheses but that didn't help.
What do I need to change to get the response to return my desired results?
On a "between", the LOWER number needs to be on the left and the GREATER number on the right. Negative numbers obviously work the opposite (greatest absolute value actually belongs on the LEFT)
change to
between -122.501212 and -122.398942

Radius Query with vender radius restrictions

I have the following radius query below. The first select will get me every record within 4 miles. (just to keep things simple, the zip in this example is just a primary key and should not be thought of as an actual zip. I'm just trying to model this as a vendor within a given long/lat.)
5 records are returned within 4 miles of latitude 43 and longitude- -74.37
What I'm looking to do is once the records are found, then do a reverse select on the records found using the restriction column as my new radius restriction to eliminate records where vendors do not want to be found outside a particular distance from their physical location.
In my query example below, I'm expecting to find all results except fonda 12068 because that record although is within 4 miles of the first select only wants to be visible within 1 mile (restriction column) of his physical location. The image however is the actual results produced. Can someone help me to understand what I'm doing wrong?
My thought was I could use the the long/lat along with the restriction from the result of the first query to see if a zip was returned matching the original zip.
SELECT zip1.* FROM zip_detail as zip1
WHERE (3958*3.1415926*sqrt((latitude-43)*(latitude-43)
+ cos(latitude/57.29578)*cos(43/57.29578)
*(longitude- -74.37)*(longitude- -74.37))/180)
<= 4
and zip1.zip in (SELECT zip2.zip FROM zip_detail as zip2
WHERE (3958*3.1415926*sqrt((latitude-zip1.latitude)*(latitude-zip1.latitude)
+ cos(latitude/57.29578)*cos(zip1.latitude/57.29578)
*(longitude- zip1.longitude)*(longitude- zip1.longitude))/180)
<= zip1.restriction)
I created a sqlfiddle example

Problem with incorrect polygon - Postgis query doesn't work

Well, I have 2 tables, one represents polygons and the second points. I want to know 2 things, the first is the area of the polygons of the first table, the second is the area of the intersection between 2 geometries:
1) the area of the polygons of the first table
2) the area of the union of a buffer of 150 meters radius around my points that is situated inside the polygons.
I made the query to do it, but something went wrong because if I set the WHERE clause to select just one of the polygons of the first table then all works fine, but if I want the data of all polygons, then I get the error: "Error performing intersection: TopologyException: Input geom 0 is invalid: Self-intersection at or near point 12.20304662098798 46.117661703823636 at 12.20304662098798 46.117661703823636"
The first query that works is :
SELECT residenziali.code,
ST_Area(residenziali.the_geom::geography, true) as area_zona,
ST_Area(ST_Intersection(residenziali.the_geom,
(ST_Union(ST_Buffer(fermate.the_geom::geography, 150)::geometry)))::geography,true) as intersect
FROM residenziali
JOIN fermate ON st_contains(residenziali.the_geom , fermate.the_geom)
WHERE residenziali.id = '969'
GROUP BY residenziali.code,residenziali.the_geom
when I remove the WHERE clause it doesn't work anymore, what is the error??
the geometry column of both tables is "the_geom", the first table is "residenziali" and the second is "fermate", both using WGS84. Thanks!!!

Paging Results: Handling invalid passed data

Have a simple page that pulls results from MySQL and displays them in a table. I have enabled paging on the results, and allowed the user to set the number of results being displayed per page. I am passing two querystring values to handle this: 'page' and 'count'.
I am then taking these values to calculate the LIMIT's of my MySQL query, using the SQL_CALC_FOUND_ROWS directive and following that with a call to SELECT FOUND_ROWS(); to get the total number of results. This all works nicely.
Now, I want to validate the querystring values. As I am storing the possible "correct" values for the results/page value of 'count' in an array, I simply check that the passed 'count' value is in that array, and if not set it to the default value. For the 'page' value, I am having a bit of a mental block... in order to determine if there are any results for the passed 'page', meaning it is "correct", I need to go to the database and find the result count first, but since I only want to go to the db once, I need to include the LIMIT's, which are based on the passed 'page' value... chicken and egg. I have a couple thoughts on how to solve this:
Run the query as coded above, and if the (('page' - 1) * 'count') result is greater than the value returned from SELECT FOUND_ROWS();, re-run the query with new LIMIT's set to 0, count.
Get the full result set, verify that the passed page is correct, then do another pull from the database with the LIMIT values.
I'd rather not go back to the database at all, but as I mentioned, having a mental block on this rather common issue.
Thanks,
Paul
I ended up using the first solution above -
Run the query as coded above, and if the (('page' - 1) * 'count') result is greater than the value returned from SELECT FOUND_ROWS();, re-run the query with new LIMIT's set to 0, count.
It's not perfect in that a second database pull is required for cases where the passed page value is bad, but given that is an unexpected case only triggered by the intentional passage of bad data on the part of the user, it's acceptable. If anyone else has a better solution, I'd be happy to re-open the question.