Correct Geojson bestand d.m.v. Python script om een extractie uit BAG panden te maken

Ik wil door middel van een Python script een selectie maken uit de BAG van de aanwezige panden in een specifiek gebied in Nederland. De output wil ik wegschrijven naar een geojson file die ik vervolgens weer kan inlezen in een geografisch informatie systeem. Bijvoorbeeld QGIS. Als ik onderstaande script uitvoer krijg ik netjes een respons. Alleen is dit geen output die ik kan inlezen als geojson bestand. Zo ontbreken FeatureCollection / Features. Wat doe ik verkeerd? Ik heb de api key even op xxx gezet. Maar deze is in mijn code correct ingevuld.

Code

import requests
import json
import geojson
from pprint import pprint

url = “https://bag.basisregistraties.overheid.nl/api/v1/panden”

payload = “{“geometrie”:{“within”:{“type”:“Polygon”,“coordinates”:[[[4.861791, 52.335380], [4.868483, 52.335407], [4.868955, 52.332312], [4.861083, 52.331984], [4.861791, 52.335380]]]}}}”
headers = {
‘X-Api-Key’: “xxxxx”,
‘Accept’: “application/hal+json”,
‘Accept-Charset’: ‘UTF-8’,
‘Content-Type’: “application/json”,
‘Cache-Control’: “no-cache”,
‘Postman-Token’: “0b46b88b-e586-416e-8c1b-1bd8b1723cc7”
}

response = requests.request(“POST”, url, data=payload, headers=headers)

data = response.json()
with open(‘data.json’, ‘w’) as f:
json.dump(data, f)

pprint(response.json())

Voorbeeld output

{’_embedded’: {‘panden’: [{’_embedded’: {‘geldigVoorkomen’: {’_embedded’: {‘bron’: {‘documentdatum’: ‘2014-03-28’,
‘documentnummer’: ‘GV00001867_AC00AC’}},
‘aanduidingCorrectie’: 0,
‘aanduidingInactief’: False,
‘beginGeldigheid’: ‘2014-03-28T00:00:00.000+01:00’,
‘eindGeldigheid’: None,
‘geconstateerd’: False,
‘inOnderzoek’: False,
‘label’: ‘Voorkomen ’
‘Pand ’
‘0363100012184477 ’
‘van ’
‘2014-03-28’},
‘geometrie’: {‘coordinates’: [[[4.8614975,
52.3339057],
[4.8614994,
52.3339058],
[4.8615012,
52.333906],
[4.861503,
52.3339064],
[4.8615046,
52.3339069],
[4.8615062,
52.3339075],
[4.8615076,
52.3339082],
[4.8615089,
52.333909],
[4.8615099,
52.33391],
[4.8615108,
52.333911],
[4.8615115,
52.333912],
[4.8615119,
52.3339131],
[4.8615121,
52.3339142],
[4.8615121,
52.3339154],
[4.8615119,
52.3339165],
[4.8615114,
52.3339176],
[4.8615107,
52.3339186],
[4.8615098,
52.3339196],
[4.8615087,
52.3339205],
[4.8615074,
52.3339214],
[4.861506,
52.3339221],
[4.8615044,
52.3339227],
[4.8615028,
52.3339231],
[4.861501,
52.3339235],
[4.8614992,
52.3339237],
[4.8614973,
52.3339238],
[4.8614955,
52.3339237],
[4.8614937,
52.3339235],
[4.8614919,
52.3339231],
[4.8614903,
52.3339226],
[4.8614887,
52.333922],
[4.8614873,
52.3339213],
[4.861486,
52.3339204],
[4.861485,
52.3339195],
[4.8614841,
52.3339185],
[4.8614834,
52.3339175],
[4.861483,
52.3339164],
[4.8614827,
52.3339152],
[4.8614827,
52.3339141],
[4.861483,
52.333913],
[4.8614835,
52.3339119],
[4.8614842,
52.3339108],
[4.8614851,
52.3339098],
[4.8614862,
52.3339089],
[4.8614874,
52.3339081],
[4.8614889,
52.3339074],
[4.8614904,
52.3339068],
[4.8614921,
52.3339063],
[4.8614939,
52.333906],
[4.8614957,
52.3339058],
[4.8614975,
52.3339057]]],
‘crs’: {‘properties’: {‘name’: ‘urn:ogc:def:crs:EPSG::4258’},
‘type’: ‘name’},
‘type’: ‘Polygon’}},
‘_links’: {‘self’: {‘href’: ‘https://bag.basisregistraties.overheid.nl/api/v1/panden/0363100012184477?geldigOp=2018-09-08’},
‘verblijfsobjecten’: {‘href’: ‘https://bag.basisregistraties.overheid.nl/api/v1/verblijfsobjecten?pandrelatering=0363100012184477&geldigOp=2018-09-08’},
‘voorkomens’: {‘href’: ‘https://bag.basisregistraties.overheid.nl/api/v1/panden/0363100012184477/voorkomens?geldigOp=2018-09-08’}},
‘identificatiecode’: ‘0363100012184477’,
‘oorspronkelijkBouwjaar’: 2008,
‘status’: ‘NietGerealiseerdPand’},
{’_embedded’: {‘geldigVoorkomen’: {’_embedded’: {‘bron’: {‘documentdatum’: ‘2014-03-28’,
‘documentnummer’: ‘GV00001867_AC00AC’}},
‘aanduidingCorrectie’: 0,
‘aanduidingInactief’: False,
‘beginGeldigheid’: ‘2014-03-28T00:00:00.000+01:00’,
‘eindGeldigheid’: None,
‘geconstateerd’: False,
‘inOnderzoek’: False,
‘label’: 'Voorkomen ’
'Pand ’
'0363100012184476 ’
'van ’
‘2014-03-28’},

Ik weet niet of je uit de BAG API ook GeoJSON kunt opvragen, mogelijk een header optie?

In ieder geval wel via de BAG WFS bijv door te POSTen naar:
https://geodata.nationaalgeoregister.nl/bag/wfs?
met bijv in POST body:

<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0" outputFormat="JSON" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><wfs:Query typeName="feature:pand" srsName="EPSG:4258"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:Intersects><ogc:PropertyName/><gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4258"><gml:exterior><gml:LinearRing><gml:posList>4.861791  52.335380 4.868483 52.335407 4.868955  52.332312 4.861083 
 52.331984 4.861791  52.335380</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></ogc:Intersects></ogc:Filter></wfs:Query></wfs:GetFeature>

en dan als antwoord:

{
  "type": "FeatureCollection",
  "totalFeatures": 28,
  "features": [
    {
      "type": "Feature",
      "id": "pand.2078429",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                4.86282736,
                52.33414574,
                0
              ],
              [
                4.86282769,
                52.33408283,
                0
              ],
              [
                4.86296707,
                52.33408311,
                0
              ],
              [
                4.86296674,
                52.33414602,
                0
              ],
              [
                4.86282736,
                52.33414574,
                0
              ]
            ]
          ]
        ]
      },
      "geometry_name": "geometrie",
      "properties": {
        "identificatie": 363100012246315,
        "bouwjaar": 2015,
        "status": "Pand in gebruik",
        "gebruiksdoel": null,
        "oppervlakte_min": 0,
        "oppervlakte_max": 0,
        "aantal_verblijfsobjecten": 0,
        "actualiteitsdatum": null
      }
    },
    {
      "type": "Feature",
      "id": "pand.4257715",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                4.86626698,
                52.33247645,
                0
              ],
              [
                4.86621417,
                52.33247637,
                0
              ],
              [
                4.86621428,
                52.33245067,
                0
              ],
              [
                4.86626709,
                52.33245075,
                0
              ],
              [
                4.86626698,
                52.33247645,
                0
              ]
            ]
          ]
        ]
      },
      "geometry_name": "geometrie",
      "properties": {
        "identificatie": 363100012224841,
        "bouwjaar": 1967,
        "status": "Pand in gebruik",
        "gebruiksdoel": null,
        "oppervlakte_min": 0,
        "oppervlakte_max": 0,
        "aantal_verblijfsobjecten": 0,
        "actualiteitsdatum": null
      }
    },
    {
      "type": "Feature",
      "id": "pand.4257716",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                4.86312997,
                52.3327692,
                0
              ],
              [
                4.86310563,
                52.33276916,
                0
              ],
              [
                4.86310565,
                52.33276359,
                0
              ],
              [
                4.86291596,
                52.33276332,
                0
              ],
              [
                4.86291593,
                52.33276889,
                0
              ],
              [
                4.86289159,
                52.33276885,
                0
              ],
              [
                4.86289186,
                52.33269904,
                0
              ],
              [
                4.86289265,
                52.33249347,
                0
              ],
              [
                4.8628927,
                52.33247855,
                0
              ],
              [
                4.86292205,
                52.33247859,
                0
              ],
              [
                4.8630045,
                52.33247872,
                0
              ],
              [
                4.86313108,
                52.3324789,
                0
              ],
              [
                4.86313104,
                52.33248828,
                0
              ],
              [
                4.86313102,
                52.33249382,
                0
              ],
              [
                4.86312997,
                52.3327692,
                0
              ]
            ]
          ]
        ]
      },
      "geometry_name": "geometrie",
      "properties": {
        "identificatie": 363100012224845,
        "bouwjaar": 1967,
        "status": "Pand in gebruik",
        "gebruiksdoel": null,
        "oppervlakte_min": 0,
        "oppervlakte_max": 0,
        "aantal_verblijfsobjecten": 0,
        "actualiteitsdatum": null
      }
    },
    {
      "type": "Feature",
      "id": "pand.6152970",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
.
.

De BAG API geeft geen GeoJSON terug. Hier kun je inderdaad beter de WFS voor gebruiken.

Hartelijk dank voor jullie antwoorden. Ik ga aan de slag met de WFS.

1 like