Skip to content

Instantly share code, notes, and snippets.

@breinbaas
Created June 19, 2022 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breinbaas/6a58de3615b036504df66df24040be37 to your computer and use it in GitHub Desktop.
Save breinbaas/6a58de3615b036504df66df24040be37 to your computer and use it in GitHub Desktop.
import requests
import xml.etree.ElementTree as ET
# this is the url for the search request to find cpts based on some query parameters
# in this case the registrationPeriod and the given area
url = "https://publiek.broservices.nl/sr/cpt/v1/characteristics/searches?requestReference=request"
# setup the query params (literal copy of the bro example in the docs)
my_obj = {
"registrationPeriod": {"beginDate": "2017-01-01", "endDate": "2021-01-01"},
"area": {
"enclosingCircle": {
"center": {"lat": 52.038297852, "lon": 5.31447958948},
"radius": 0.5,
}
},
}
# get it..
x = requests.post(url, json=my_obj)
# oh dear.. xml.. not my strong suit.. please.. start using json! ;-)
# call to all XML hero's.. the next code is awful.. maybe you know a better way?
root = ET.fromstring(x.text)
broIds = []
for child in root:
if child.tag == "{http://www.broservices.nl/xsd/dscpt/1.1}dispatchDocument":
for elem in child:
for e in elem.findall("{http://www.broservices.nl/xsd/brocommon/3.0}broId"):
broIds.append(e.text)
# ok, we got the id's and we can use the API's get method to get the files
for broId in broIds[:1]: # to spare the bro server let's keep it at one file for now
url = f"https://publiek.broservices.nl/sr/cpt/v1/objects/{broId}"
resp = requests.get(url)
print(resp.text) # there you go!
@breinbaas
Copy link
Author

the bare minimum.. you should add the error handling and dynamic search queries yourself!

@snakesonabrain
Copy link

Great stuff! Dutch CPTs might be on the menu for my courses soon ;-). Is the XML schema available somewhere?

@snakesonabrain
Copy link

By the way, I used the xmltodict library to get to the data I want in a slightly more Pythonic way. Dictionary keys still look awful though...

@breinbaas
Copy link
Author

I know that Thomas van der Linden did the tedious work of dissecting the XML format; see https://github.com/Amsterdam/gefxml_viewer/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment