Combine Socrata Latitude and Longitude columns to form Point-type Location column - socrata

I'm trying to figure out how to use the within_circle(...) function with the Socrata-based Food Establishment Inspection data set. Unfortunately it appears the data set does not have the required Point datatype column but rather two Number columns with Latitude and Longitude. How can I create a virtual/calculated Point-type Location column based on Latitude and Longitude so I can use within_circle(...)?

Related

One query showing the first nearest and second nearest points between coordinates

I have the following tables:
A postcode_lookup table which lists the latitude and longitude for different postcodes. And a contact and event table which include names of the contacts / events and their postcodes.
I've greyed out postcodes which would not be found in the postcode_lookup table.
I want to perform a query which will list the 1st nearest and 2nd nearest event for each contact. The results should look something like the following:
I'm having trouble figuring out how to do this.
I know you can get the individual latitude, longitudes for a contact / event with something like
SELECT
contact.id,
contact.name,
latitude AS lat1,
longitude AS long1
FROM
contact
LEFT JOIN
postcode_lookup ON contact.postcode = postcode_lookup.postcode
And you can get the distance between two coordinates with
SELECT ST_DISTANCE_SPHERE(POINT(lat1, long2), POINT(lat2, long2)) AS distance
But I'm not sure how to combine everything together into one query which will give something like the desired results shown in the above example

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

mysql distance between search

so I have two tabels where one has a list of all postcodes with long and lat and the other has store data with long and lat.
What I need to know is how do I find all the stores that match the long and lat from a postcode within 30km
Use the Haversine formula (MySQL implementation) to calculate the distance between them. Return rows which have a distance of less or equal to 30.
Review this http://jan.ucc.nau.edu/~cvm/latlon_formula.html for formula, and put the formula on select statement and order by the distance

Sorting results on a mysql table along longitude and then latitude, NOT a circular radius

Hey all...
I've been tasked with a global search project.
They want to search a database of pictures, all with long/lat
references storied per image in a mysql DB.
However, their search criteria is a little specific.
A simplified table structure is:
id INT auto inc primary
imagePath varchar 255
long DECIMAL 12,7
lat DECIMAL 12,7
I need to provide the SQL statement with a start long/lat position.
They then want to receive 34 images, going south from their initial
long reference.. once they get to -90 (the bottom of the earth) to pop
over 1 degree to the right/East and search up to the north to 90 and
repeat until 34 images are found.
What's confusing me is how to calculate the minus to plus changes..
ensuring that we loop up and down along the earths 'long' reference.
Has anyone done this before..?
I'd LOVE to do it in a single query...
(I've seen how one can fetch results based on a circle radius, but my client.. bless em.. doesn't want to do that)
Ok, back to google!!
Thoughts...
Tris...
Inputs are $the_lat and $the_long
SELECT *
FROM table
WHERE long > $the_long
OR ( long = $the_long AND lat >= $the_lat)
ORDER BY long ASC, lat ASC
LIMIT 34
As is, it's a uni-directional search -- it won't wrap around to longs < $the_long.