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.

from crabpy.client import crab_factory

crab = crab_factory()

res = crab.service.ListGemeentenByGewestId(1)

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 has been provided to make life easier.

from crabpy.client import capakey_factory, capakey_request

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

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

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 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_request

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 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_request

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.