Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Use new PDOK api for location search
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdstruijk committed Jul 28, 2023
1 parent e9f5f37 commit 7b95733
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions viewer/src/main/java/nl/b3p/viewer/search/PDOKSearchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.io.ParseException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.geotools.geometry.jts.WKTReader2;
import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -43,10 +43,10 @@
*/
public class PDOKSearchClient extends SearchClient {

private static final Log log = LogFactory.getLog(SolrSearchClient.class);
private SolrServer server;
private static final Log log = LogFactory.getLog(SolrSearchClient.class);
private WKTReader2 wkt;
private String filter;
private static final String PDOK_URL="https://api.pdok.nl/bzk/locatieserver/search/v3_1/free";

/* This is a lookup table with a distance per type of object so we can generate a sensible
bbox to zoom to, these values are used when we only get a point for a hit.
Expand All @@ -66,7 +66,6 @@ public class PDOKSearchClient extends SearchClient {
}

public PDOKSearchClient(String filter){
server = new HttpSolrServer("http://geodata.nationaalgeoregister.nl/locatieserver/v3");
wkt = new WKTReader2();
this.filter = filter;
}
Expand All @@ -76,30 +75,45 @@ public SearchResult search(String term) {
SearchResult result = new SearchResult();
try {
JSONArray respDocs = new JSONArray();
SolrQuery query = new SolrQuery();
// add asterisk to make it match partial queries (for autosuggest)
term += "*";
if(this.filter != null){
query.setFilterQueries(this.filter);
String final_url = this.PDOK_URL + "?q=" + term;
if(this.filter != null && !this.filter.isEmpty()){
final_url += "&fq=" + this.filter;
}
query.setQuery(term);

// specify fields to retrieve (null values wil be omitted in the response),
// the default is listed at https://github.com/PDOK/locatieserver/wiki/API-Locatieserver#52url-parameters
// this list is probably still longer than needed, so maybe could be pruned
query.setParam("fl", "identificatie,weergavenaam,bron,type,openbareruimte_id,openbareruimtetype,straatnaam,adresseerbaarobject_id,nummeraanduiding_id,huisnummer,huisletter,huisnummertoevoeging,huis_nlt,postcode,woonplaatscode,woonplaatsnaam,gemeentenaam,provinciecode,provincienaam,kadastraal_object_id,kadastrale_gemeentecode,kadastrale_gemeentenaam,kadastrale_sectie,perceelnummer,kadastrale_grootte,gekoppeld_perceel,kadastrale_aanduiding,centroide_rd,boundingbox_rd,geometrie_rd,score");
query.setRequestHandler("/free");
QueryResponse rsp = server.query(query);
SolrDocumentList list = rsp.getResults();

for (SolrDocument solrDocument : list) {
JSONObject doc = solrDocumentToResult(solrDocument);
final_url += "&fl=" + "identificatie,weergavenaam,bron,type,openbareruimte_id,openbareruimtetype,straatnaam,adresseerbaarobject_id,nummeraanduiding_id,huisnummer,huisletter,huisnummertoevoeging,huis_nlt,postcode,woonplaatscode,woonplaatsnaam,gemeentenaam,provinciecode,provincienaam,kadastraal_object_id,kadastrale_gemeentecode,kadastrale_gemeentenaam,kadastrale_sectie,perceelnummer,kadastrale_grootte,gekoppeld_perceel,kadastrale_aanduiding,centroide_rd,boundingbox_rd,geometrie_rd,score";
URL url = new URL(final_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
StringBuilder response = new StringBuilder();
String line;
try(BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException ex) {
log.error("Cannot get search response from PDOK:",ex);
}

JSONObject pdokResponse = new JSONObject(response.toString()).getJSONObject("response");
JSONArray docs = pdokResponse.getJSONArray("docs");

Iterator<Object> it = docs.iterator();
while (it.hasNext()) {
JSONObject doc = (JSONObject) it.next();
doc = solrDocumentToResult(doc);
if (doc != null) {
respDocs.put(doc);
}
}

result.setResults(respDocs);
result.setLimitReached(list.getNumFound() > list.size());
} catch (SolrServerException ex) {
result.setLimitReached(pdokResponse.getInt("numFound") > docs.length());
} catch ( IOException ex) {
log.error("Cannot search:",ex);
}
return result;
Expand All @@ -111,10 +125,10 @@ public JSONArray autosuggest(String term) throws JSONException {
return r.getResults();
}

private JSONObject solrDocumentToResult(SolrDocument doc){
private JSONObject solrDocumentToResult(JSONObject doc){
JSONObject result = null;
try {
Map<String, Object> values = doc.getFieldValueMap();
Map<String, Object> values = doc.toMap();
result = new JSONObject();
for (String key : values.keySet()) {
switch (key) {
Expand All @@ -129,7 +143,7 @@ private JSONObject solrDocumentToResult(SolrDocument doc){
}
}

String geom = (String) doc.getFieldValue("centroide_rd");
String geom = (String) doc.get("centroide_rd");
if (values.containsKey("geometrie_rd")) {
geom = (String) values.get("geometrie_rd");
} else if (values.containsKey("boundingbox_rd")) {
Expand Down

0 comments on commit 7b95733

Please sign in to comment.