Search four fields in YQL geo.places - geolocation

We are currently using YQL to query geo data for towns and counties in the UK. At the moment, we can use the following query to find all towns named Boston:
select * from geo.places where text="boston" and placeTypeName="Town"
Demo
The issue is, that we would like to specify the county and country to generate more specific results. I have tried the following query, but it returns 0 results:
select * from geo.places where (text="boston" and placeTypeName="Town") and (text="lincolnshire" and placeTypeName="County")
Demo
How can I query 3 field types to return the results I need? Essentially, we would like to query the following fields:
text and placeTypeName="Town"
text and placeTypeName="County"
text and placeTypeName="Country"

This may be an option maybe:
https://developer.yahoo.com/blogs/ydnsevenblog/solving-location-based-services-needs-yahoo-other-technology-7952.html
As it mentions:
Turning text into a location
You can also turn a text (name) into a location using the following code:
yqlgeo.get('paris,fr',function(o){
alert(o.place.name+' ('+
o.place.centroid.latitude+','+
o.place.centroid.longitude+
')');
})
This wrapper call uses our Placemaker Service under the hood and automatically disambiguates for you. This means that Paris is Paris, France, and not Paris Hilton; London is London, England, and not Jack London.

Related

How to count cypher labels with specific condition?

I have a graph database with information about different companies and their subsidiaries. Now my task is to display the structure of the company. This I have achieved with d3 and vertical tree.
But additionally I have to write summary statistics about the company that is currently displayed. Companies can be chosen from a dropdown list which is fetching this data dynamically via AJAX call.
I have to write in the same HTML a short summary like :
Total amount of subsidiaries for CompanyA: 300
Companies in Corporate Havens : 45%
Companies in Tax havens 5%
My database consists of two nodes: Company and Country, and the country has label like CH and TH.
CREATE (:TH:Country{name:'Nauru', capital:'Yaren', lng:166.920867,lat:-0.5477})
WITH 1 as dummy MATCH (a:Company), (b:Country) WHERE a.name=‘CompanyA ' AND b.name='Netherlands' CREATE (a)-[:IS_REGISTERED]->(b)
So how can I find amount of subsidiaries of CompanyA that are registered in corporate and tax havens? And how to pass this info further to html
I found different cypher queries to query all the labels as well as apocalyptic.stats but this does not allow me to filter on mother company. I appreciate help.
The cypher is good because you write a query almost in natural language (the query below may be incorrect - did not check, but the idea is clear):
MATCH (motherCompany:Company {name: 'CompanyA'})-[:HAS_SUBSIDIARY]->(childCompany:Company)
WITH motherCompany,
childCompany
MATCH (childCompany)-[:IS_REGISTERED]->(country:Country)
WITH motherCompany,
collect(labels(country)) AS countriesLabels
WITH motherCompany,
countriesLabels,
size([countryLabels IN countriesLabels WHERE 'TH' IN countryLabels ]) AS inTaxHeaven
RETURN motherCompany,
size(countriesLabels) AS total,
inTaxHeaven,
size(countriesLabels) - inTaxHeaven AS inCorporateHeaven

rails - count records by value

I am trying to group records based on a value from a column, so I can use it to display the information elsewhere. At the moment I have this working if I specify the values in the column -
#city_count = People.select('city,count(*)').where("city in ('london', 'paris')").group(:city).count
This works fine if I want a list of people in London and Paris but if the city list also has Sydney, New York, Rio etc I don't want to keep adding the extra cities to the 'city in', I would like this to just find the people selected by each city.
Does anyone know the best way of doing this? Also if it can include NULL values as well.
Just use:
#city_count = People.group(:city).count
to get counts for all cities. This will include an entry for nil.
A more efficient way would be to use the distinct and count methods together.
#city_counts = Person.distinct.count(:city)
That way the work is done in the db instead of in Ruby.

In Factual how to get results with unique field values (similar to GROUP BY in SQL)?

I've just set out on the path to discovery of Factual API and I cannot see how to achieve a retrieval of a selection of entries each with a unique value in the specified field.
For example, give me 10 results from various cities:
q.limit(10);
q.field("locality").unique(); // no such filter exists
factual.fetch("places", q);
This would be an equivalent query in MySQL:
SELECT * FROM places GROUP BY locality LIMIT 10;
What I want is a little bit similar to facets:
FacetQuery fq = new FacetQuery("locality").maxValuesPerFacet(10);
fq.field("country").isEqual("gb");
FacetResponse resp = factual.fetch("places", fq);
but instead of the total for each result I would like to see a random object with all the information.
Is anything like this possible?

How to get areas/regions for a country and cities for this area/region?

We made a simple query which gets some cities:
SELECT * FROM `allCountries` WHERE name='Moscow' and `country_code` = 'RU'
Here is the result of this query:
For example, for another city we get a result with 4-7 rows.
How to get all areas/regions for a country and then get all cities for this area/region?
P.S.: Please be careful. We are not interested in an API site and database fetch. Thanks!
Background
In Geonames you have feature_classes and feature_codes which discriminate the location type. You can find detailed description of the code in the Geonames website. As in your snapshot, P.PPLC means "City (populated place) which is capital of a political entity" and S.HLC means "building (spot) hotel".
Also, every geoname have properties to identify the location in the "hierarchy" inside a country; this properties are country_code, admin1_code, admin2_code, admin3_code, admin4_code. Note that not all properties are used for every given geoname, since this depends on the political organization of a country.
Find all city inside an administrative level
To find all city inside an area (i.e. administrative level), you must first search the geoname for that admin level, in order to have the admin codes useful to filter the city query.
To find an admin level, you must first execute a query like:
SELECT *
FROM `allCountries`
WHERE `country_code` = 'RU'
AND `feature_class`='A'
AND `feature_code`='ADM1'
Note that the query filter out only the first admin levels (feature_code='ADM1'), but you can find admin level of any depth by changing it to:
SELECT *
FROM `geonames`
WHERE `country_code` = 'RU'
AND `feature_class`='A'
AND `feature_code` LIKE 'ADM_'
Now, select one record from this result set and you it to search for the cities, by using the "hierarchy" codes of that level. You should use something like (mutatis mutandis):
SELECT *
FROM `geonames`
WHERE `country_code` = "RU"
AND `feature_class`='P'
AND `feature_code` LIKE 'PPL%'
AND `admin1_code`="<admin1>"
AND `admin2_code`="<admin2>"
AND `admin3_code`="<admin3>"
AND `admin4_code`="<admin4>"
Beware of NULL admin codes, which you need to strip out from the SQL (the whole "AND ..." clause).
Of course, you can do the original "Moscow" search inside this filtered set.
The answer for your question is pretty long, but this code snippet may help you a little bit. These queries obtain all hierarchy information about given geonameid (it's plpython inside postgres).
get_geoname = plpy.prepare("SELECT geonameid, asciiname, country, admin1, admin2 FROM all_countries where geonameid=$1",
["integer"])
get_country_name = plpy.prepare("SELECT name as country from country_info where code = upper($1)", ["varchar"])
get_admin1 = plpy.prepare("SELECT asciiname, name FROM admin1 where code = $1", ["text"])
get_admin2 = plpy.prepare("SELECT asciiname, name FROM admin2 where code = $1", ["text"])

Jquery autocomplete to search within more than one columns of a table

Jquery UI's autocomplete has given the solution to searching through one column of a table. So if you get the source from a table with firstname field then you can only search with firstname.
But say you have more than one fields to search against like firstname, lastname, postcode, contactNumber. Then in that case how would you implement something like this for autosuggest.
I mean that user should be able to search with whatever field they like and the autosuggest should be able to give them the suggestions based on that.
Is this possible?
The source can be an array, a string or a function.
You'd have to write a custom function to read all the values in all the columns of the tables and store them in an array. Then call the .source method with your array.
For bonus points, a series of checkboxes above the search field would ask the user to search only those columns.
[ ] FirstName [X] Lastname [X] Department
_Smith_____
John Smith IT
Waylon Smithers Assistant to the Assistant Regional Manager
Jackie Brown Gunsmith

Resources