Skip to content

Geodata

Darwin Monroy edited this page Oct 4, 2017 · 1 revision

This wiki page describes how to:

  • list all existing geodatums from crunch api
  • load the geodata entity, searching by name
  • manage the geodata view on a variable

List & Retrieval of geodata

We've added 2 helpers for list and retrieval of geodata from crunch api. Import before use it.

>>> from scrunch.datasets import list_geodata, get_geodata

The list_geodata() method returns the geodata objects from crunch api, is a python dictionary with geodata name as key and geodata entity as value.

>>> list_geodata()
{
  'US Census Regions': {
    'description': '',
    'created': '2016-10-16T02:49:10.708000+00:00',
    'name': 'US Census Regions',
    'location': 'https://s.crunch.io/geodata/crunch-io/cb_2015_us_region_20m.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '067e865ee4384d51809f37bee7b0a75c',
    'metadata': {}
  },
  'US States GeoJSON Postal Code (stabbr)': {
    'description': '',
    'created': '2016-07-08T16:33:44.601000+00:00',
    'name': 'US States GeoJSON Postal Code (stabbr)',
    'location': 'https://s.crunch.io/geodata/leafletjs/us-states.geojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '7ae898e210b04a9a8992314452c6677b',
    'metadata': {}
  },
  'GB PDL Region': {
    'description': 'use properties.EER13NM',
    'created': '2016-07-25T22:05:07.471000+00:00',
    'name': 'GB PDL Region',
    'location': 'https://s.crunch.io/geodata/yougov/gb_eer_pdl_profile_GOR.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '9583e54953cb443ead75200137f439c1',
    'metadata': {}
  },
  'London Plan Regions': {
    'description': 'properties.Name',
    'created': '2016-12-14T20:07:11.620000+00:00',
    'name': 'London Plan Regions',
    'location': 'https://s.crunch.io/geodata/crunch-io/lp-consultation-oct-2009-subregions.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '97cef15e695c415b9c92bf5d137f5842',
    'metadata': {}
  },
  'NaturalEarth World Admin0': {
    'description': 'The entire world in 50m',
    'created': '2016-08-19T20:04:30.557000+00:00',
    'name': 'NaturalEarth World Admin0',
    'location': 'https://s.crunch.io/geodata/naturalearth/ne_50m_admin_0_countries/ne_50m_admin_0_countries.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '8f9f5fed101042c4815d2dd1fd248cec',
    'metadata': {}
  },
  'US States Topojson': {
    'description': 'properties.name or properties.postal-code',
    'created': '2016-11-02T21:20:29.389000+00:00',
    'name': 'US States Topojson',
    'location': 'https://s.crunch.io/geodata/leafletjs/us-states.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '94910acc200c4c25868ffc9d50f35655',
    'metadata': {}
  },
  'Scottish border': {
    'description': '',
    'created': '2017-08-10T21:17:14.322000+00:00',
    'name': 'Scottish border',
    'location': 'https://s.crunch.io/geodata/UK-GeoJSON-part/json/eurostat/simplified/scotland.geojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '9e39de215faa4b379a6bf73a5f6ef159',
    'metadata': {}
  },
  'UK LAD': {
    'description': 'local administrative districts',
    'created': '2016-06-27T08:05:52.498000+00:00',
    'name': 'UK LAD',
    'location': 'https://s.crunch.io/geodata/UK-GeoJSON-part/json/administrative/gb/topo_lad.json',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '70c0e1c111884fecb58a245268450ef7',
    'metadata': {}
  },
  'UK Regions': {
    'description': '',
    'created': '2015-12-10T18:37:50.479000+00:00',
    'name': 'UK Regions',
    'location': 'https://s.crunch.io/geodata/UK-GeoJSON-part/json/eurostat/simplified/regions.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': 'd878d8471090417fa361536733e5f176',
    'metadata': {}
  },
  'US 115th Congress': {
    'description': 'properties.geoid should be cassregfullcd',
    'created': '2017-06-22T14:40:35.651000+00:00',
    'name': 'US 115th Congress',
    'location': 'https://s.crunch.io/geodata/crunch-io/us-115th-congress.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '8f2c0e89983c4c8fa8463bc313658b12',
    'metadata': {}
  },
  'GB Regions': {
    'description': 'GB European Electoral Regions (no NI)',
    'created': '2016-06-27T12:25:14.687000+00:00',
    'name': 'GB Regions',
    'location': 'https://s.crunch.io/geodata/yougov/gb_eer.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '2b64724ce81c41c9bdc2436fb0bf6026',
    'metadata': {}
  }
}

And use the get_geodata() function to load the geodata entity, given a geodata name.

>>> get_geodata('GB PDL Regions')
{
  'element': 'shoji:entity',
  'self': 'https://app.crunch.io/api/geodata/9583e54953cb443ead75200137f439c1/',
  'description': 'Details for a type of geodata',
  'body': {
    'description': 'use properties.EER13NM',
    'format': 'geojson',
    'created': '2016-07-25T22:05:07.471000+00:00',
    'name': 'GB PDL Region',
    'location': 'https://s.crunch.io/geodata/yougov/gb_eer_pdl_profile_GOR.topojson',
    'owner_id': '8e01e9f7a8e944a7a2c88e50c2a2997d',
    'id': '9583e54953cb443ead75200137f439c1',
    'metadata': {
      'status': 'success',
      'properties': {
        'EER13NM': [
          'North East',
          'North West',
          'Yorkshire and the Humber',
          'East Midlands',
          'West Midlands',
          'East of England',
          'London',
          'South East',
          'South West',
          'Scotland',
          'Wales'
        ],
        'EER13CDO': [
          '01',
          '02',
          '03',
          '04',
          '05',
          '06',
          '07',
          '08',
          '09',
          '11',
          '10'
        ],
        'EER13CD': [
          'E15000001',
          'E15000002',
          'E15000003',
          'E15000004',
          'E15000005',
          'E15000006',
          'E15000007',
          'E15000008',
          'E15000009',
          'S15000001',
          'W08000001'
        ]
      }
    }
  }
}

Manage geodata view on a variable

Assuming we have a variable that represents all GB Regions (a categorical variable with regions as categories)

{
  'alias': 'location',
  'name': 'Where do you live?',
  'type': 'categorical',
  'categories': [
    {
      'id': 1,
      'missing': False,
      'name': 'Protestant',
      'numeric_value': 1
    },
    {
      'id': 2,
      'missing': False,
      'name': 'North East',
      'numeric_value': 2
    },
    {
      'id': 3,
      'missing': False,
      'name': 'North West',
      'numeric_value': 3
    },
    {
      'id': 4,
      'missing': False,
      'name': 'Yorkshire and the Humber',
      'numeric_value': 4
    },
    {
      'id': 5,
      'missing': False,
      'name': 'East Midlands',
      'numeric_value': 5
    },
    {
      'id': 6,
      'missing': False,
      'name': 'West Midlands',
      'numeric_value': 6
    },
    {
      'id': 7,
      'missing': False,
      'name': 'East of England',
      'numeric_value': 7
    },
    {
      'id': 8,
      'missing': False,
      'name': 'London',
      'numeric_value': 8
    },
    {
      'id': 9,
      'missing': False,
      'name': 'South East',
      'numeric_value': 9
    },
    {
      'id': 10,
      'missing': False,
      'name': 'South West',
      'numeric_value': 10
    },
    {
      'id': 11,
      'missing': False,
      'name': 'Wales',
      'numeric_value': 11
    },
    {
      'id': 12,
      'missing': False,
      'name': 'Scotland',
      'numeric_value': 12
    },
    {
      'id': 13,
      'missing': False,
      'name': 'Northern Ireland',
      'numeric_value': 13
    }
  ]  
}

First, load the dataset

ds = get_dataset("Dataset X")

Now load the variable

location = ds['location']

The variable object has the set_geodata_view(...) and unset_geodata_view() methods to enable and disable the geodata view for the variable, use it as follow:

The function set_geodata_view receives the following parameters:

  • geodata: url, name or Entity of the geodatum to use
  • feature_key: key defined for each Feature in the geojson/topojson that matches the relevant field on the variable

To set geodata by name do the following:

>>> location.set_geodata_view('UK Regions', feature_key='EER13NM')

Now confirm that the geodata view has been set:

>>> location.view['geodata']
[
  {
    'rendering_type': None,
    'display_field': 'name',
    'variable_id': '000008',
    'geodatum': 'https://app.crunch.io/api/geodata/d878d8471090417fa361536733e5f176/',
    'feature_key': 'EER13NM',
    'match_field': 'name'
  }
]

Then, to disable/remove the geodata view for the variable just call the unset_geodata_view() method

>>> location.unset_geodata_view()

You can also set the geodata by url

>>> location.set_geodata_view(
    'https://app.crunch.io/api/geodata/d878d8471090417fa361536733e5f176/', 
    feature_key='EER13NM'
)

Or using the geodata entity

>>> geodata = get_geodata('UK Regions')
>>> location.set_geodata_view(geodata, feature_key='EER13NM')
Clone this wiki locally