ckan / ckanext-mapviews

CKAN Resource View to build maps and choropleth maps
26 stars 23 forks source link

Set multiple filters when redirecting to another page #10

Closed vitorbaptista closed 10 years ago

vitorbaptista commented 10 years ago

Given that we have the following data:

country state city population
USA New York New York City 8336697
USA New York New York 19651127
USA USA USA 317600000

I want to build a navigatable map of the USA that, when clicking on New York City, I would be redirected to a dashboard with the data filtered to it. But I don't want only the city. I would also like to compare it to the state at large, and the country.

First of all, we need a way to define the hierarchy between the geographical regions (country, state, and city in this example). We do so simply by repeating them in each row.

With that solved, now we need a way to plot all the data in a single chart. At first, we thought of letting the user pick which columns she wants to filter (for example, country, state, and city), and when clicking on a city we would get its columns and add the relative filters. In that case, we would end up with country:USA|state:New York|city:New York City.

Unfortunately, that won't work because our filters are all AND. There's only one row that's country == "USA" AND state == "New York" AND city == "New York City", and that's the row we clicked in the first place. The only way to set OR clauses would be to have them all use the same column name. That's why we ended up setting everything for everyone. For example, the country USA's row would define state as USA and city as USA.

Then, when setting up the filters, we get the state and country from the city clicked on, but use a single column name: city.

There's one caveat to this: there can't be a city with the same name as the state (or country). If there is, you need to disambiguate them by adding (city) or something. For example, New York and New York City.

vitorbaptista commented 10 years ago

There's another problem with this approach: if we create a bar chart (using https://github.com/ckan/ckanext-basiccharts) for all New York's cities, we'll end up plotting the New York state as well. As there's currently no way to exclude some row (only to include), we can't remove this.

If we're plotting rates (as in literacy rate or something), it isn't a big deal. Might be useful, actually. But if we're plotting population, things will get ugly. Check the following example:

population-without-province population-with-province

These images are plots on the population of the province Khyber Pakhtunkhwa province's districts from Pakistan. In the first one, we plot just the districts; in the second, we plot the province as well. As the province has much more people than each district, the scales get screwed. Things get worse if we add the country.

To solve this, we can add a type or region_type to the CSV, which defines what that row is. Our first example up there would become:

country state city population region_type
USA New York New York City 8336697 city
USA New York New York 19651127 state
USA USA USA 317600000 country

So we just need to add a filter region_type=city :thumbsup: