How to fetch GPS coordinates of world's largest cities from WikiData via SPARQL, so that the result set includes Paris? - gis

By searching on Google and SO, I came up with the following SPARQL query for finding world's largest cities for the purpose of rudimentary geocoding:
SELECT ?city ?cityLabel ?countryLabel ?iso ?population ?gps
WHERE {
?city wdt:P31 wd:Q515 . hint:Prior hint:runFirst true .
?city wdt:P17 ?country .
?country wdt:P297 ?iso .
?city wdt:P625 ?gps .
?city wdt:P1082 ?population .
FILTER (?population > 100000) .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)
LIMIT 5000
https://query.wikidata.org/#SELECT%20%3Fcity%20%3FcityLabel%20%3FcountryLabel%20%3Fiso%20%3Fpopulation%20%3Fgps%0AWHERE%20%7B%0A%20%20%3Fcity%20wdt%3AP31%20wd%3AQ515%20.%20hint%3APrior%20hint%3ArunFirst%20true%20.%0A%20%20%3Fcity%20wdt%3AP17%20%3Fcountry%20.%0A%20%20%3Fcountry%20wdt%3AP297%20%3Fiso%20.%0A%20%20%3Fcity%20wdt%3AP625%20%3Fgps%20.%0A%20%20%3Fcity%20wdt%3AP1082%20%3Fpopulation%20.%0A%20%20FILTER%20(%3Fpopulation%20%3E%20100000)%20.%0A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22.%20%7D%0A%7D%0AORDER%20BY%20DESC(%3Fpopulation)%0ALIMIT%205000
For some reason, the result set does not include Paris (France) but includes smaller cities in France. What am I doing wrong?
Thank you!

?city wdt:P31 wd:Q515 .
This triple pattern excludes Paris (Q90), because it’s not an instance of city (Q515).
It’s an instance of subclasses of city (Q515), though. For example: capital city (Q5119).
To find all items that are instances of city (Q515) or of a subclass of city (Q515), you can use a property path:
wdt:P31/wdt:P279*
P31: instance of
/: SequencePath
P279: subclass of
*: ZeroOrMorePath
As a city can be an instance of multiple city subclasses, you might want to make the results distinct, otherwise these cities will appear multiple times in the result:
SELECT DISTINCT ?city ?cityLabel #etc.

Related

Geocoder.geocode({'placeId': placeId}) doesn't return administrative_area_level_2/1

I have problem with result of Geocoder.geocode({'placeId': placeId}) on specific location
(i.e. Main Street 34, Paris) then I will get address_components: street,sublocality,locality,country,postal_code.
function geocodePlaceId(geocoder, placeId, callback) {
geocoder.geocode({'placeId': placeId}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[0]) {
console.log(results[0]);
callback(results[0].address_components);
}
});
}
code above for the precise location (which contains street nmbr. street and city) returns address_components that are not important for me.
However I want administrative_area_level_2 and administrative_area_level_1 types. Unfortunately I googled a lot and nothing. I thought that this definition for my request can be helpfull
https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderComponentRestrictions but not.
Example: https://maps.googleapis.com/maps/api/geocode/json?&address=luka%20666%20brusno
I created request with exact address with street, number, city and returned address_components which contains only these keys: route, locality, country and postal_code,
but if i change a request to .. address=brusno result contains administrative_area_level_2 and administrative_area_level_1. How can i got administrative_area_level_1 and administrative_area_level_2 for first option?
Do you have some idea how can I resolve this issue or can I influence returned types of address_components?
Based on the Google Maps Geocoding documentation, administrative_area_level_# indicates a civil entity below the country level. And not all nations exhibit these administrative levels. In most cases, administrative_area_level_# short names will closely match ISO 3166-2 subdivisions and other widely circulated lists; however this is not guaranteed as our geocoding results are based on a variety of signals and location data. So maybe that is the issue.
Also, regarding about the request using only the address=brusno, you will notice here that the administrative_area_level_# here is in the Banská Bystrica District not in the place that you use like Luka, Brusno.
If you query that address you will notice that it is the same result.
using address=Banská Bystrica District
https://maps.googleapis.com/maps/api/geocode/json?&address=Bansk%C3%A1%20Bystrica%20District
So maybe like the documentation state that not all place(Luka, Brusno)exhibit these administrative levels.

Groupby array by multiple values

I have an array I want to use groupby angular's filter and to group it by an array of criterias. Is there any way to do this? I guess I might use revision but I don't know how.
For example I have array of football teams. Each team have color, number of players, name, city, country.
I have array of data filters -country, city, players and color and I want to use groupby in the order of this array
not sure about angular. but just include underscore (it's more known for array/object manipulation) into your project then:
say you had an array of objects like an array of these:
car = {
make: "nissan",
model: "sunny",
color: "red"
};
then you'd just go:
var redCars = _.groupBy(cars, 'color');

Region/counties google place codes?

Any one know in which ISO format google places returns the place object short_name for regions ?
I have this code:
var place = places.getPlace();
var type = place.address_components[0].types[0];
switch(type) {
case 'country': document.getElementById('country').value=place.address_components[0].short_name;
break;
case 'locality':
document.getElementById('town').value=place.address_components[0].long_name;
break;
default: //when is not a country or a city
document.getElementById('regioncode').value=place.address_components[0].short_name
;
In case is a region/county it gets the shor_name of it, but is some cases it returns a code like searching for "Balearic Islands, Spain" it return "PM" (which stands for ISO 3166-2:ES), but if I search "Kalmar län, Sweden" I get exactly the same for short_name as long_name which is "Kalmar County" instead of "H" (which stands for ISO 3166-2:SE)so my question is: Why Google sometimes return ISO 3166-2 values for regions/counties and sometimes the long_name value, is it any logic behind this? Can I force to ONLY return ISO values for short_names ??? thank you
According to Google Documentation:
short_name is an abbreviated textual name for the address component,
if available. For example, an address component for the state of
Alaska may have a long_name of "Alaska" and a short_name of "AK" using
the 2-letter postal abbreviation.
The Key word here is "if available". Kalmar County must not have a short name

Uitableview, json and detail tableview

I am asking a very simple question for someone who has encountered this kind of problem, please don't mark it down because there are many approaches to a solution.
I have a database which has cities and town, cities has many town, therefore, using one query displays many cities to correspond to the town. The result is as follows from mysql;
{City - town 1, town2, town 3} therefore if I want to display both the town and cities, I will end up with {city-town1, city-town2, city-town3}
I want to be able to compile all towns under a city, so that I can eventually use this source of array for my drilldown table, i.e on the first table; I want a list of non-repeating cities, on detail table, a list of all towns under that city. I only want to use one table and make the array dynamic such that I can add cities and towns without any problems (I have seen examples proposing different tables for different arrays)
I used this for- loop to iterate through mysql result
for (int i=0; i<json.count; i++)
{
ids= [[json objectAtIndex:i] objectForKey:#"Id"];
cityName = [[json objectAtIndex:i] objectForKey:#"cityName"];
townName = [[json objectAtIndex:i] objectForKey:#"townName"];
}
This gives me a list of cities but they are repeating;
I have been thinking about different approaches;
1)use #distinctunion- (Key-Value Coding)
2)use two jsons (one for city and another for towns) -compile arrays under each then join them using common id
3)use an iteration, iterate as shown above, but also search for similar cityNames and group towns under cities
Has anyone encountered a similar situation? what did you do? could you give an example if possible please
This is JSON Result for NSMutable Array of the above iteration
2013-09-18 07:50:52.025 JSONDATA[7119:11603] (
Bath,
"Bath, Somerset"
)
2013-09-18 07:50:52.035 JSONDATA[7119:11603] (
Bath,
"Bath, Somerset"
)
2013-09-18 07:50:52.035 JSONDATA[7119:11603] (
Bath,
Keynsham
)
2013-09-18 07:50:52.035 JSONDATA[7119:11603] (
Bath,
Keynsham
)

Development of railway enquiry system, how to model Trains, Stations and Stops?

For example, if I have two trains, X and Y and they travel:
TRAIN.........STATIONS
Train-X : Goes Via Station-A; Station-B; Station-C; Station-D
Train-Y : Goes Via Station-B; Station-X; Station-D; Station-Y
How would I go about putting this information in a database so that if a passenger inquired about What trains start as Station-B? and What trains End as Station-D? then both Train-X and Train-Y should come in the result.
I would say you need to have three tables to make this work.
Station: station ID, name, etc.
Service: service ID, operator, number of train cars perhaps, etc.
Service_Stop: service ID, stop number, station ID.
You can then find services that stop at Station-B and subsequently at Station-D using a query something like the following:
SELECT
Service_ID
FROM
Station AS Start_Station
JOIN Service_Stop AS Start_Stop ON
Start_Station.Station_ID = Start_Stop.Station_ID
JOIN Service_Stop AS End_Stop ON
Start_Stop.Service_ID = End_Stop.Service_ID AND
Start_Stop.Stop_Number < End_Stop.Stop_Number
JOIN Station AS End_Station ON
End_Stop.Station_ID = End_Station.Station_ID AND
End_Station.Name = "Station-D"
WHERE
Start_Station.Name = 'Station-B'
I would use five tables:.
Train: train_id, name # e.g "The UnionT522"
Station: station_id, name # e.g. "Eggles Place"
Route: route_id, route_name # e.g. "Afternoon special at 4pm"
RouteStation: route_station_id, route_id, station_id, route_order, begin_flag, end_flag
TrainRoute: train_route_id, train_id, route_id # e.g. which train (above) is going on which route (above).
So RouteStation would have whether things began or ended at a given station for a given route.
TrainRoute would have information about which route a train is taking.