Fetch request vs sparql query

Hello,

Could anyone tell me what the difference is between the resulting building bag ids from this javascript fetch request and this sparql query? They don’t seem to match correctly.

js fetch:

export async function fetchBuildingsByCoordinates(coords: { latitude: number; longitude: number }) {
    const url = 'https://bag.basisregistraties.overheid.nl/api/v1/panden'

  return fetch(url, {
   method: 'POST',
  headers: {
    'X-Api-Key': 'my-api-key',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    geometrie: { contains: { type: 'Point', coordinates: [coords.latitude, coords.longitude] } },
  }),
})
  .then((r) => r.json())
  .then((r) => {
    return r
  })
  }

sparql query:

def get_address_and_building_bag_ids(postal_code: nil, house_number: nil)
  query = %Q{
    prefix bag: <http://bag.basisregistraties.overheid.nl/def/bag#>
    SELECT ?addressBagId ?buildingBagId WHERE {
              ?object a bag:Verblijfsobject .
              ?object bag:hoofdadres ?nummer .
              ?object bag:identificatiecode ?addressBagId .
              ?nummer bag:postcode ?postcode .
              ?nummer bag:huisnummer ?huisnummer .
              ?object bag:pandrelatering ?pand .
              ?pand bag:identificatiecode ?buildingBagId .
              FILTER (?postcode = "#{postal_code}" && ?huisnummer = #{house_number.to_i})
    }
  }
  results = bag_sparql_get(query)
  results.map do |result|
    {
      address_bag_id: result["addressBagId"]["value"],
      building_bag_id: result["buildingBagId"]["value"]
    }
  end
end

def bag_sparql_get(query)
  url = URI("https://bag.basisregistraties.overheid.nl/sparql")
  response = HTTParty.post(url, headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'application/sparql-results+json'
    }, 
    body: URI.encode_www_form({ query: query }))
  begin
    JSON.parse(response.body)["results"]["bindings"]
  rescue => e
    raise "Could not fetch BAG SPARQL #{e.message}, query: #{query}"
  end
end

The JS fetch is a request searching for panden with a spatial filter (the geometrie in the requestbody).

The SPARQL query returns a pand id and a verblijfsobject id related to a nummeraanduiding with a postal code and house number.

Besides the fact that their responses will both include the id of a pand, these queries are completely different.

Can you explain why you expect the outcomes to match?

Apologies for not being more specific in my initial post. What I should have asked is, why do the pand ids not match in the response of those two requests. Although, after more investigation it appears that they do match.

My development team and I have been developing a map where we match our buildings and the buildings on the map based on the id / ids of the pand. Initially we were calling the api from fetch requests on the front end with a JS fetch request. We made the requests using an addresses geocoded coordinates which we got using a geocoding library. However, now we have decided to do it on the back end with SPARQL queries using only an addresses house number and post code. It appeared as if the two requests were returning different pand ids, but after more debugging it seems that we have a problem with the geocoding library we are using. We found coordinates located in Utrecht for an address in Leeuwarden. So from the geocoded coordinates request we would sometimes get a different pand ids than from the SPARQL query.

Anyways, I guess that all means that this post is a bit irrelevant. But hey, you live and you learn! Thanks for the response @RobinTopper !

2 likes