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.
"""
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.
"""
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)
Be careful: the CAPAKEY SOAP gateway is deprecated. We advise you the use the CAPAKEY REST gateway instead.
Using a client behind 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()
.
"""
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 crab more pythonic, we’ve also implemented a gateway that abstracts some more of the service and provides richer objects as responses.
"""
This script demonstrates using the crab gateway to walk the entire
address tree (street and number) of a `gemeente`.
"""
from crabpy.client import 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)
for sa in h.subadressen:
print("\t\t* %s" % sa)
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.
"""
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.
"""
This script demonstrates using the capakey gateway to walk the entire
cadastral tree of a `gemeente`.
WARNING: The CapakeyGateway (SOAP) is deprecated, use CapakeyRestGateway (REST) instead.
"""
from crabpy.client import capakey_factory
from crabpy.gateway.capakey import CapakeyGateway
capakey = capakey_factory(user="USER", password="PASSWORD")
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.
"""
This script demonstrates querying the capakey gateway while maintaining a cache.
WARNING: The CapakeyGateway (SOAP) is deprecated, use CapakeyRestGateway (REST) instead.
"""
import os
from crabpy.client import capakey_factory
from crabpy.gateway.capakey import CapakeyGateway
capakey = capakey_factory(user="USER", password="PASSWORD")
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("--------------------------------")
s = g.get_sectie_by_id(44021, "A")
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.
Warning
Be careful: the CAPAKEY SOAP gateway is deprecated. We advise you the use the CAPAKEY REST gateway instead.
Using the CAPAKEY REST gateway¶
To make life easier and capakey more pythonic, we’ve also implemented a rest gateway that abstracts some more of the service and provides richer objects as responses.
The CAPAKEY REST gateway supports caching through the dogpile caching library. Caching can
be added by passing a configuration dictionary to the CapakeyRestGateway
.
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.
"""
This script demonstrates querying the capakey gateway while maintaining a cache.
"""
import os
from crabpy.gateway.capakey import CapakeyRestGateway
root = "./dogpile_data/"
if not os.path.exists(root):
os.makedirs(root)
g = CapakeyRestGateway(
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.