How to find the name of a place from latitude and longitude value - sql-server

I am analysing app data which contains lat value and lon value of a user visited places. I was able to export the data to tableau and plot it on the map but I want to find the name of place for each pair of lat and lon.
One solution could be, if I get a table of three columns (Lat, Lon, Place) then I can join it with my user data table to find the name of a place at a given Lat and Lon.
My question is, do we have a ready made table with the above three columns which I can import in my SQL-Server? I am interested in places of UK or London. Is there any other approach to achieve it?

You can get this from the Ordinance Survey which should get you lat, long, postcode;
https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html
You'll then need another data source to map the postcode to location name (e.g. town, county etc). See the similar post below;
Where can I find a list of all UK _full_ postcodes including street name and their precise coordinates?
It might take a little fiddling about, and you're always going to have the issue with data being a little out of date but it should be good enough.

I wrote an API wrapper in R for postcodes.io, which is a free UK postcode database. Check the original documentation so that you could create an API wrapper in your language of choice. Wrappers in languages other than R are also available.
If you use R, then type you can get the place names in the following way:
if (!require("devtools")) install.packages("devtools")
devtools::install_github("erzk/PostcodesioR")
library(PostcodesioR)
rev_geo <- reverse_geocoding(0.127, 51.507)
It will return a list with extensive information about the latitude and longitude, e.g. wards, NUTS, administrative district, county, parish, consituency, CCG and many more.
There is also a bulk_reverse_geocoding() function which takes several lat and lon inputs.

Related

Can you create dynamic formulas in Google Sheets?

So I'm just starting out creating a portfolio tracker within Google Sheets. I'm using the Google Finance methods to get the stocks name and all the relevant data that I need. The only issue is that I can't figure out how to populate the specific data I need without having to manually type out the same formula's for each stock I want data for.
For example... Each row in the first column would contain the ticker symbol for that specific stock. If I bought a new stock, I would just type in the ticker symbol in cell A1 and this would populate the necessary fields such as price and so on. If I bought another stock I would essentially do the same thing but now in A2.
I know that you can get the price of a stock by doing
=GOOGLEFINANCE(A1, "price")
but is there any way to make it dynamic? something like:
=GOOGLEFINANCE(A(Row(ref)), "price")?
Any suggestions would be helpful. Maybe there's even an addon that makes this process simpler, but I'm not sure.
try:
=ARRAYFORMULA(IFERROR(GOOGLEFINANCE(A1:A10, "price")))
You just have to write the function for A1:
=GOOGLEFINANCE(A1, "price")
And then drag the little square on the cell down. It will automatically pick up the correspondant number of the row in the A column.
You can set-up your sheet to have like 100 rows used, and when you add the ticker it will automatically calculate it.
If you don't want th #N/A to show you can do it like:
=IFERROR(GOOGLEFINANCE(A1, "price"))

Ride Sharing App - finding surrounding origins and destinations

I looked at several other SO questions that seem somewhat related, but not quite what i need (or i'm just not smart enough to connect the dots).
Working on an app for a client. Their database holds the origin and destination of people that are traveling, limited (i believe) to just places in US and Canada, and a date when the trip will take place. The records are updated regularly. Call these "trips."
Users come to the site, and enter an origin and destination city, and a radius for each, indicating how far away from their desired origin/destination cities they are willing to travel in order to make their trip.
The job of the app is to find any/all trips that are already in the database, that are closest to the origin and destination that the user needs to travel.
My original thought was to find all origin cities in the database that are within the radius of the user's desired origin, then use that recordset to search the destination cities in the database for any/all cities within the radius of the user's desired destination.
I also need a decent (preferably free... low budget project here) API that can help look up the city geographic location and perform the actual radius calculation... I think.
Is what I'm looking to do even close to the best options? It looks like the hardest part will be finding all the existing cities in the database that are within the radius of the user's desired cities - which is a bit of a twist on a more simple query of just "find all cities in the radius of X city".
So, this is KINDA like an Uber situation, except the Uber driver is deciding what the trip parameters are, and the user just needs to know which Uber drivers are going from/to the places nearest those of the user (on the specified date, to boot).
Right now, users are just looking things up at a state level - BC to NY, and reading down rows of data looking at rides to find the ones that seem closest to what they need.
Thanks in advance, for any clever insights you smart folks might have!
Declare #DriverLat float = 41.744068
Declare #DriverLng float = -71.315024
Declare #Within int = 20
Select *
From (
Select Distinct
A.ZipCode
,A.CityName
,A.StateCode
,Miles = [dbo].[udf-Geo-Calc-Miles] (#DriverLat,#DriverLng,A.Lat,A.Lng)
From [dbo].[ZipCodes] A
Where CityType = 'D'
and ZipType = 'S'
) A
Where Miles <= #Within
Order By Miles
Returns
The UDF
CREATE Function [dbo].[udf-geo-Calc-Miles] (#Lat1 float,#Lng1 float,#Lat2 Float,#Lng2 float)
Returns Float as
Begin
Declare #Miles Float = (Sin(Radians(#Lat1)) * Sin(Radians(#Lat2))) + (Cos(Radians(#Lat1)) * Cos(Radians(#Lat2)) * Cos(Radians(#Lng2) - Radians(#Lng1)))
Return Case When #Miles is null then 0 else abs((3958.75 * Atan(Sqrt(1 - power(#Miles, 2)) / #Miles))) end
End

What Data Structure is best suited to store differences

I have a set of pre defined properties I want to store. For example:
PersonNr,Gender,Name,Surname, Address, Zip,City.
Now I have different sources for these data sets, which share the PersonNr but have different value for the other properties:
EXAMPLE:
From Database A I get
123456,M,Hudson,James,Fakestr 123, 12345, West City
From Database B i get
123456,M,Hudson,Jameson,Fakestr123, 12345, East City
Instead of storing both values I want to store the data from Database A as a reference and only store the data from B that are different to A.
In my Example I would like to store something like:
Database B, Jameson, East City
What data structure can I use for the given problem?
Thanks in advance
The solution you choose depends a lot on the nature of your data, how you're going to store it, and what you want to do with it. If all you want is an abbreviated record that only stores the deltas, then you could write a comma-separated line that has empty fields. That is, given:
Database A
123456,M,Hudson,James,Fakestr 123, 12345, West City
Database B
123456,M,Hudson,Jameson,Fakestr123, 12345, East City
You could write a separate record showing the deltas:
123456,,,Jameson,,,East City
If you're storing the deltas in a database, then you'll probably want records that give the record identifier, the field name, and the changed value. That representation would be:
123456,Surname,Jameson
123456,City,East City
That's probably how I'd represent it in memory, too: a hash map keyed by record identifier (i.e. 123456), with a list of field name / value pairs for each ID.

Get Longitude and Latitude from Country Name

I have Gmaps embedded in my site which pulls out a list of dealers from my database around the world and plots their location with a marker based on the dealer postcode in Gmaps. This works great however I need to extend this functionality to support dealers who do not have a postcode but rather, they are assigned to Countries.
So for example, Test Dealer could be assigned to Germany and Belgium so I need to somehow get a generic longitude and latitude value for the country names "Germany" and "Belgium" and then show a marker for those long/lat values.
Is this possible?
If so, where do I start?!
Thanks for reading.
kris
Try using google's geocoder. Just provide a generic address, in your case a country and it will return the center point of that country together with the coordinates(latitude and longitude)
Here's a link. https://developers.google.com/maps/documentation/javascript/geocoding

Movement Paths & Spatial-Temporal Queries in SQL Server

Hey, so I'm trying to figure out the best way of storing movement paths and then afterwards how they might be queried.
Let me try to explain a bit more. Say I have many cars moving around on a map and I want to determine if and when they're in a convoy. If I store just the paths then I can see that they travelled along the same road, but not if they were there at the same time. I can store the start and end times but that will not take into account the changes in speed of the two vehicles. I can't think of any obvious way to store and achieve this so I thought I'd put the question out there in case there's something I'm missing before trying to implement a solution. So does anyone know anything I don't?
Thanks,
Andrew
Well it depends on what type of movement information you have.
If you have some tables setup like:
Vehicle (Id, Type, Capacity, ...)
MovementPoint(VehicleId, Latitude, Longitude, DateTime, AverageSpeed)
This would allow you to query if two cars going to the same point plus or minus 5 minutes like so:
Select * from Vehicle v INNER JOIN MovementPoint mp on mp.VehicleId = v.Id
WHERE v.Id = #FirstCarID
AND EXISTS
(
SELECT 1 FROM Vehicle v2 INNER JOIN MovementPoint mp2 on mp2.VehicleId = v2.Id
WHERE v2.Id = #SecondCarId
AND mp2.Latitude = mp.Latitude AND mp2.Longitude = mp.Longitude
AND mp2.DateTime BETWEEN DATEADD(minute,-5,mp.DateTime) AND DATEADD(minute,5,mp.DateTime)
)
You could also query for multiple points in common between multiple vehicles with specific time windows.
Also you could make the query check latitude and longitude values are within a certain radius of each other.

Resources