Using CRABpy

Using the CRAB webservice

Recently, the CRAB service has become public. The need to authenticate has been removed, making it a whole lot easier to connect. A utility function crabpy.client.crab_request() has been provided, similar to crabpy.client.capakey_request(). This allows for a slightly different way of calling methods on the service.

# -*- coding: utf-8 -*-
'''
This script demonstrates using the crab client directly or through the
:func:`crabpy.client.crab_request` function.
'''

from crabpy.client import crab_factory, crab_request

crab = crab_factory()

res = crab.service.ListGemeentenByGewestId(1)
print(res)

res = crab.service.ListPostkantonsByGemeenteId(71)
print(res)

res = crab_request(crab, 'ListGemeentenByGewestId', 1)
print(res)

res = crab_request(crab, 'ListHuisnummersWithStatusByStraatnaamId', 18618)
print(res)

res = crab_request(crab, 'GetStraatnaamWithStatusByStraatnaamId', 18618)
print(res)

Using the CAPAKEY webservice

This service does still require authentication. This requires a valid account from agiv. Because the authentication also requires some extra WS-Addressing headers, a utility function crabpy.client.capakey_request() has been provided to make life easier.

# -*- coding: utf-8 -*-
'''
This script demonstrates using the capakey client through the
:func:`crabpy.client.capakey_request` function.
'''

from crabpy.client import capakey_factory, capakey_request

capakey = capakey_factory(
    user='USER',
    password='PASSWORD'
)

res = capakey_request(capakey, 'ListAdmGemeenten', 1)
print(res)

res = capakey_request(capakey, 'ListKadAfdelingenByNiscode', 44021, 1)
print(res)

res = capakey_request(capakey, 'ListKadSectiesByKadAfdelingcode', 44021)
print(res)

res = capakey_request(capakey, 'ListKadPerceelsnummersByKadSectiecode', 44021, 'A', 1)
print(res)

res = capakey_request(capakey, 'GetKadPerceelsnummerByCaPaKey', '44021A3675/00A000')
print(res)

Using a client behing a proxy

If you need to connect to CRAB or CAPAKEY through a proxy, you can do so by passing the proxy parameter to the crabpy.client.crab_factory() or crabpy.client.capakey_factory().

# -*- coding: utf-8 -*-
'''
This script show how to connect to the WS-WRAB service through a proxy.
'''

from crabpy.client import crab_factory

crab = crab_factory(
    proxy={
        'http': 'http://proxy.example.com:3128',
        'https': 'https://httpsproxy.example.com:3128'
    }
)

print(crab.service.ListGemeentenByGewestId(1))

Using the CRAB gateway

To make life easier and carab more pythonic, we’ve also implemented a gateway that abstracts some more of the service and provides richer objects as responses.

# -*- coding: utf-8 -*-
'''
This script demonstrates using the crab gateway to walk the entire
address tree (street and number) of a `gemeente`.
'''

from crabpy.client import crab_request, crab_factory
from crabpy.gateway.crab import CrabGateway

g = CrabGateway(crab_factory())

gemeente = g.get_gemeente_by_id(1)

print(str(gemeente))
for s in gemeente.straten:
    print("* %s" % s)
    for h in s.huisnummers:
        print("\t* %s" % h)

The CRAB gateway supports caching through the dogpile caching library. Caching can be added by passing a configuration dictionary to the CrabGateway.

Three caching regions will be configured:

  • permanent: For requests that can be cached for a very long time, eg. list_gewesten or list_gemeenten.
  • long: For requests that can be cached for a fairly long time, eg. list_straten.
  • short: For requests that will only be cached for a little while, eg. get_huisnummer_by_id.
# -*- coding: utf-8 -*-
'''
This script demonstrates querying the crab gateway while maintaining a cache.
'''

import os

from crabpy.client import crab_factory

from crabpy.gateway.crab import CrabGateway

root = "./dogpile_data/"

if not os.path.exists(root):
    os.makedirs(root)

g = CrabGateway(
    crab_factory(),
    cache_config={
        'permanent.backend': 'dogpile.cache.dbm',
        'permanent.expiration_time': 604800,
        'permanent.arguments.filename': os.path.join(root, 'crab_permanent.dbm'),
        'long.backend': 'dogpile.cache.dbm',
        'long.expiration_time': 86400,
        'long.arguments.filename': os.path.join(root, 'crab_long.dbm')
    }
)


aartselaar = g.get_gemeente_by_id(1)

print('Straten in AARTSELAAR')
print('---------------------')
print([str(s) for s in g.list_straten(aartselaar)])
    
print('Huisnummers in AARTSELAAR Straat1')
print('---------------------------------')
print([str(h) for h in g.list_huisnummers_by_straat(s)])

p = g.get_gemeente_by_niscode(33021)

print('gemeente: %s' % p.id)
print('naam: %s' % p.naam)
print('niscode: %s' % p.niscode)
print('gewest: %s' % p.gewest)
print('provincie: %s' % p.provincie)
print('taal: %s' % p.taal)
print('centroid: %s' % str(p.centroid))
print('bounding_box: %s' % str(p.bounding_box))

Using the CAPAKEY gateway

To make life easier and capakey more pythonic, we’ve also implemented a gateway that abstracts some more of the service and provides richer objects as responses.

# -*- coding: utf-8 -*-
'''
This script demonstrates using the capakey gateway to walk the entire
cadastral tree of a `gemeente`.
'''

from crabpy.client import capakey_factory

capakey = capakey_factory(
    user = 'USER',
    password = 'PASSWORD'
)

from crabpy.gateway.capakey import CapakeyGateway

g = CapakeyGateway(capakey)

gemeente = g.get_gemeente_by_id(45062)

print(str(gemeente))
for a in gemeente.afdelingen:
    print("* %s" % a)
    for s in a.secties:
        print("\t* %s" % s)
        for p in s.percelen:
            print("\t\t* %s" % p)

The CAPAKEY gateway supports caching through the dogpile caching library. Caching can be added by passing a configuration dictionary to the CapakeyGateway.

Three caching regions will be configured:

  • permanent: For requests that can be cached for a very long time, eg. list_gemeenten.
  • long: For requests that can be cached for a fairly long time, eg. list_secties_by_afdeling.
  • short: For requests that will only be cached for a little while, eg. get_perceel_by_capakey.

Please bear in mind that in this case short can probably be fairly long. We suspect that the database underlying the capakey service is not updated that regularly, so a short caching duration could easily be one hour or even a day.

# -*- coding: utf-8 -*-
'''
This script demonstrates querying the capakey gateway while maintaining a cache.
'''

import os

from crabpy.client import capakey_factory

capakey = capakey_factory(
    user = 'USER',
    password = 'PASSWORD'
)

from crabpy.gateway.capakey import CapakeyGateway

root = "./dogpile_data/"

if not os.path.exists(root):
    os.makedirs(root)

g = CapakeyGateway(
    capakey,
    cache_config = {
        'permanent.backend': 'dogpile.cache.dbm',
        'permanent.expiration_time': 604800,
        'permanent.arguments.filename': os.path.join(root, 'capakey_permanent.dbm'),
        'long.backend': 'dogpile.cache.dbm',
        'long.expiration_time': 86400,
        'long.arguments.filename': os.path.join(root, 'capakey_long.dbm'),
        'short.backend': 'dogpile.cache.dbm',
        'short.expiration_time': 3600,
        'short.arguments.filename': os.path.join(root, 'capakey_short.dbm')
    }
)

gent = g.get_gemeente_by_id(44021)

print('Afdelingen in Gent')
print('------------------')

print([str(a) for a in g.list_kadastrale_afdelingen_by_gemeente(gent)])

print('Secties in GENT AFD 1')
print('---------------------')

print([str(s) for s in g.list_secties_by_afdeling(44021)])

print('Percelen in GENT AFD 1, Sectie A')
print('--------------------------------')

print([str(p) for p in g.list_percelen_by_sectie(s)])

print('Perceel 44021A3675/00A000')
print('-------------------------')

p = g.get_perceel_by_capakey('44021A3675/00A000')

print('perceel: %s' % p.id)
print('capakey: %s' % p.capakey)
print('percid: %s' % p.percid)
print('grondnummer: %s' % p.grondnummer)
print('bisnummer: %s' % p.bisnummer)
print('exponent: %s' % p.exponent)
print('macht: %s' % p.macht)
print('sectie: %s' % p.sectie)
print('afdeling: %s'% p.sectie.afdeling)

See the examples folder for some more sample code.