Commit f6a974f2 authored by Alexander's avatar Alexander

working example endpoint fetching geojson from OSM overpass api

parent 490c2e09
......@@ -265,6 +265,22 @@
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/de.ixilon/overpass-api -->
<dependency>
<groupId>de.ixilon</groupId>
<artifactId>overpass-api</artifactId>
<version>0.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
</dependencies>
<build>
<finalName>geoportal</finalName>
......
package de.transformationsstadt.geoportal.api;
import de.transformationsstadt.geoportal.services.OsmAdministrativeBoundariesService;
import org.springframework.beans.factory.annotation.Autowired;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/osm-admin-boundaries/")
public class OsmAdministrativeBoundariesEndpoint {
//TODO: make an example endpoint to just return a geqjson fetched from overpass api
@Autowired
public OsmAdministrativeBoundariesService osmAdministrativeBoundariesService;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/example")
public Response ping() {
try {
String geojsonstring = osmAdministrativeBoundariesService.getRawOSM_GeoJSON_Example();
return Response.status(Response.Status.OK).entity(geojsonstring).build();
}catch (Exception e){
e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build();
}
}
}
package de.transformationsstadt.geoportal.services;
import de.ixilon.osm.api.OverpassClient;
import de.ixilon.osm.schema.OsmNode;
import de.transformationsstadt.geoportal.entities.Rectangle;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
@Service
public class OsmAdministrativeBoundariesService {
public OsmAdministrativeBoundariesService(){
//TODO: find client library to easily query Overpass
//https://wiki.openstreetmap.org/wiki/Java_Access_Example
//https://github.com/kodapan/osm-common/
//https://gitlab.com/openstreetcraft/overpass-api
String overpass_api_url ="http://overpass-api.de/api/interpreter";
String boundingBox_wuppertal_roughly="s=\"51.0\" w=\"7.0\" n=\"52.0\" e=\"8.0\"";
String querry="<osm-script output=\"json\" timeout=\"25\">\n" +
" <!-- gather results -->\n" +
" <union>\n" +
" <!-- query part for: “admin_level=8” -->\n" +
" <query type=\"node\">\n" +
" <has-kv k=\"admin_level\" v=\"8\"/>\n" +
" <bbox-query "+boundingBox_wuppertal_roughly+"/>\n" +
" </query>\n" +
" <query type=\"way\">\n" +
" <has-kv k=\"admin_level\" v=\"8\"/>\n" +
" <bbox-query "+boundingBox_wuppertal_roughly+"/>\n" +
" </query>\n" +
" <query type=\"relation\">\n" +
" <has-kv k=\"admin_level\" v=\"8\"/>\n" +
" <bbox-query "+boundingBox_wuppertal_roughly+"/>\n" +
" </query>\n" +
" </union>\n" +
" <!-- print results -->\n" +
" <print mode=\"body\"/>\n" +
" <recurse type=\"down\"/>\n" +
" <print mode=\"skeleton\" order=\"quadtile\"/>\n" +
"</osm-script>";
public String getRawOSM_GeoJSON_Example() throws Exception{
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(overpass_api_url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", querry));
post.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse res = client.execute(post);
return new String(IOUtils.toByteArray(res.getEntity().getContent()));
}
public List<Long> getIdsOfAdministrativeBoundaryRelationsInRectangle(Rectangle rect){
public List<Long> getIdsOfAdministrativeBoundaryRelationsInRectangle(Rectangle rect)throws Exception{
//TODO: query the overpass API for the references
return new ArrayList<>();
/*
<!--
This has been generated by the overpass-turbo wizard.
The original search was:
“admin_level=8”
-->
<osm-script output="json" timeout="25">
<!-- gather results -->
<union>
<!-- query part for: “admin_level=8” -->
<query type="node">
<has-kv k="admin_level" v="8"/>
<bbox-query {{bbox}}/>
</query>
<query type="way">
<has-kv k="admin_level" v="8"/>
<bbox-query {{bbox}}/>
</query>
<query type="relation">
<has-kv k="admin_level" v="8"/>
<bbox-query {{bbox}}/>
</query>
</union>
<!-- print results -->
<print mode="body"/>
<recurse type="down"/>
<print mode="skeleton" order="quadtile"/>
</osm-script>
*/
OverpassClient client = new OverpassClient(new URI(overpass_api_url));
ArrayList<Long> ids=new ArrayList<>();
for(Object obj: client.post(querry).getNodesAndRelationsAndWaies()){
if(obj instanceof OsmNode){
OsmNode node = (OsmNode)obj;
System.out.println(node.getId());
ids.add(node.getId());
}
}
return ids;
}
public List<String> getGeoJSONOfAdministrativeBoundariesInRectangle(Rectangle rect){
public List<String> getGeoJSONOfAdministrativeBoundariesInRectangle(Rectangle rect)throws Exception{
//TODO: query the overpass API for the administrative boundaries.
//TODO: simplify the resulting GeoJSON and cache it
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment