[Python] Geocoding NZ Addresses to NZTM, getting Bad Request errors

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I need to write a simple script (preferably in Python) that can take a single New Zealand street address (e.g., "28 Stanley Street, Parnell") and programmatically return its precise coordinates in the NZTM (New Zealand Transverse Mercator) format.

    Is directly querying the WFS service with a cql_filter the correct modern method for a single address lookup, or is there a simpler RESTful "Geocoding API" that I've completely missed?

    If WFS is the right approach, can anyone see a mistake in my query syntax that would cause this 400 Bad Request error?

    I have thus far tried the following APIs/Services


    • Auckland Council APIs: Initially looked at their services but ran into dead links, server timeouts, and ArcGIS REST endpoints that appear to have been decommissioned.


    • NZ Post AddressChecker API: This looked promising, but their pre-requisites require an active NZ Post business account, which I don't have for this project.


    • LINZ Data Service (LDS): This seems like the most logical and authoritative source. I have successfully registered for a free account and have generated an API key. This is where I'm currently stuck.

    My Closest Attempt (and Current Problem):

    I've been trying to query the official "NZ Addresses" dataset (layer-105688) directly using its WFS endpoint. However, my requests are being rejected.

    Here is the Python code I am using:

    import requests

    # My LDS API Key
    api_key = "PASTE_YOUR_KEY_HERE"

    # The address components I'm trying to find
    address_number = 28
    road_name = "STANLEY STREET"
    suburb = "PARNELL"

    # The LINZ WFS endpoint and the NZ Addresses layer ID
    layer_id = "105688"
    base_url = f"https://data.linz.govt.nz/services;key={api_key}/wfs"

    # Building a structured query to find the address
    cql_filter_query = (
    f"address_number={address_number} AND "
    f"full_road_name='{road_name}' AND "
    f"suburb_locality='{suburb}'"
    )

    # Setting up the request parameters
    params = {
    'service': 'WFS',
    'version': '2.0.0',
    'request': 'GetFeature',
    'typeNames': f'layer-{layer_id}',
    'outputFormat': 'application/json',
    'srsName': 'EPSG:2193', # Asking for NZTM coordinates
    'cql_filter': cql_filter_query
    }

    # Making the request
    try:
    response = requests.get(base_url, params=params)
    response.raise_for_status()
    print(response.json())
    except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")
    if 'response' in locals() and response:
    print(f"Error details: {response.text}")


    When I run this, I consistently get the following error:

    An error occurred: 400 Client Error: Bad Request for url: https://data.linz.govt.nz/services;key=.../wfs?service=WFS&version=2.0.0&request=GetFeature&typeNames=layer-105688&outputFormat=application%2Fjson&srsName=EPSG%3A2193&cql_filter=address_number%3D28+AND+full_road_name%3D%27STANLEY+STREET%27+AND+suburb_locality%3D%27PARNELL%27


    How should I formulate my request to satisfy the requirements?

    Continue reading...

Compartilhe esta Página