Database Structure for Geocoding - database

I want to try and do a geocoding application and access data stored in a SQLite database. A postcode, town or city will be supplied, and I want to return the long/lat. I want it to be screaming fast but I'm unsure about how to organise the data in the DB.
Here is the data im working with:
Town, City, Postcode, Country, Longitude, Latitude
This may be simple but im new to this, I would benefit from some advice.

People will mis-spell all of the things that you propose using as your search criteria. So, do not use strings for Town, City, Postcode, and Country in this database table.
A good first step would be to use an Id, with the Id looked up in a separate table (e.g. the Towns table). A user could then lookup Latitude and Longitude using a combination of Id's. If one or more Ids are omitted, multiple results could be returned. For example, if they only supply the Id for the Postcode and Country, that will match multiple Cities/Towns.
A more efficient method of looking up specifically Latitude and Longitude would be to assign a specific place Id to each entry in the table that holds your Latitude and Longitude
PlaceId Latitude Longitude
Allow the user to lookup the PlaceId separately (by using a combination of Town, City, Postcode, Country and pick from among multiple return values to get a specific Place). Use that PlaceId to lookup Latitude and Longitude.

Related

Address Table Scheme for Geolocation services

I have a SQL database with tables such as Stores, Users, etc.
Both Store and User, as well as other entities, have an Address.
I am considering creating an Addresses table for all Addresses:
Users
--------
UserId (PK)
AddressId (FK)
Name
Addresses
--------
AddressID (PK)
CountryID (FK) -> I have a table Countries with all Countries
Street
PostalCode
Locality
Region
Latitude
Longitude
When a user is filling the Profile I would have an input to type the address.
I would like to use Google Geocoding API to convert the typed address into its fields and save it into the database including Latitude and Longitude.
https://developers.google.com/maps/documentation/geocoding/start
https://developers.google.com/maps/documentation/javascript/geocoding
I have a few questions:
Is my Address table scheme appropriate for this?
I am planing to use Google Geocoding API so should I store the PlaceId then Google returns on my database?
My main objectives are:
Convert the address typed by the user into address fields and save to database;
By having the Latitude and Longitude of the address type by the user in my database I will be able to search them by location.
Example: Get users of my application in a radius of 20 km of a London address.
Did you thought about to separate the HouseNumber? Some external Services or API's require this. Its easier to concat this fields if required.
There is a geography Datatype for geolocations: https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/spatial-types-geography?view=sql-server-2017
With this type you can query the Database performantly with some geo functions e.g. distance between two points.
I wont save the Google-API result directly. Use your own required schema.
If you use the geography type you can query with Long/Lat and Distance by radius.

Using the SQL Server Geography data type

I am working with a SQL Server database that has a customer table in it that contains (among other things) an address column. This column houses the full address (street, city, state, zip) as one long text string. We are looking to convert this information into latitude & longitude coordinates for plotting on a map. I've been looking into using the Geography data type in SQL Server, but I cannot find any definitive information regarding what data actually needs to be entered into the Geography column in the database table.
The question I have: is there a way to use the Geography data type to convert the address string (or any part of it, City, Zip, etc.) into latitude & longitude coordinates?
For what you're looking to do, you'd need latitude and longitude information. The data type doesn't convert addresses inherently, but there are many services that you can use to do that.

many to numerous connection amongst nations and hash table

Database design problem:
I have to model a situation where I have a table Hashtag (which contains hash_id, hashtag_name, count positive and count negative and other details of the hashtag) which has to be related to two different tables cities and country.
I further classified my problem we have a database schema in which we store hashtags just like twitter site, for example if someone upload status and uses hashtag #java so we search user city and country, means java hashtag uploaded by this user in Karachi and Karachi located in Pakistan so there are many to many relations between hashtag table and cities table and same many to many relations between hashtag table and country table and county table has one too many relations with cities.
so there is a problem - one hashtag come from many cities similarly one hashtag came from many countries. Can anyone help me to simplify this problem?
A city only ever has one country (not considering border changes due to conflict, which may be something to consider when working on an international scale).
An individual instance of a hashtag can only ever originate from one city.
An individual instance of a hashtag is related to the collection of hashtags.
Table: Hashtag
HashtagID
HashtagText
Table: Country
CountryID
CountryName
Table: City
CityID
CityName
CountryID (FK to Country)
Table: HashtagUsage
HashtagID (FK to Hashtag)
CityID (FK to City)
DateTimeUsed
Every usage of a hashtag is related to only one hashtag and one city and has a timestamp of when it was used.
This is a very simplified model, but hopefully it will help you see the approach to modelling many-to-many relationships.
If your are modeling this for a date-warehouse, hashtag, city and country are dimensions, hashtagusage is the event(fact) table.

Find Longitude Latitude in Large File

I would like to know if there is a quick way of doing this.
I have a data file with over 700,000 rows. The data file gives me the attributes Country, City, and Address. I need to use this data set on a Map report. My problem is the tool needs Longitude and Latitude attributes.
My file doesn't have Longitude or Latitude. Is there a way for me to get the Longitude and Latitude for the entire data set using the current attributes without having to look up the addresses one by one on google?

City <-> Zipcode database design

I need to create a database containing all cities and zip codes worldwide. For that I want to create a table 'city' and a table zip_code. My question is, how is the relation between city and zip code, is it worldwide an 1:n relationship or can it also be m:n in some countries?
It differs. In Holland we got multiple zipcodes per street, most of the time. The zip codes are so fine grained (consisting of 4 digits + 2 letters), that just the zipcode and the address house number is enough to uniquely identify a building.
In Belgium, though, there's a 4 digit postal code, and a couple of towns can have the same zipcode, while it is still possible that a larger city has multiple zipcodes. It can even happen that a city has multiple zipcodes, while each (or some) of them are shared with some smaller towns as well.
So I would almost say that there is not relation between zip code and city, or at least no one definition that works on a global scale. If you would store it in a database, then it's definately a m:n relation.
For the US at least it is a M:M. A city can have multiplle zips, and a zip can cover more than one city, (Obviously depending on how loosly you define a city vs a town or a municipality).

Resources