Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server side): add OSM geo data importer #27

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ServerSide/resourceserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@

<dependencies>

<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>

<dependency>
<groupId>ru.servers</groupId>
<artifactId>protocol</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limita
*/

package ru.servers.resourceserver.geodata;

import ru.servers.resourceserver.network.HTTPException;

import java.io.IOException;

public interface GEOImporter {
/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add additional empty line before comment.

* latWest - latitudeWest
* lonSouth - longitudeSouth
* latEast - latitudeEast
* lonNorth - longitudeNorth
*/
String importGeoData(double latWest, double lonSouth, double latEast, double lonNorth) throws HTTPException, IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limita
*/

package ru.servers.resourceserver.geodata;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import lombok.extern.log4j.Log4j;
import ru.servers.resourceserver.network.HTTPException;
import ru.servers.resourceserver.network.HTTPStatusCode;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Log4j
public class OSMImporter implements GEOImporter {

private static String serverAddress;
private static String downloadGeoDataURL;

public OSMImporter () throws IOException{
try (InputStream inputStream = getClass().getResourceAsStream("/osm.properties")) {
Properties properties = new Properties();
properties.load(inputStream);
serverAddress = properties.getProperty("osm.serverAddress");
downloadGeoDataURL = properties.getProperty("osm.url.downloadGeoData ");
} catch (IOException e) {
throw new IOException("File with OSM server properties was not read.");
}
}

/**
* latWest - latitudeWest
* lonSouth - longitudeSouth
* latEast - latitudeEast
* lonNorth - longitudeNorth
*/
@Override
public String importGeoData(double latWest, double lonSouth, double latEast, double lonNorth) throws HTTPException {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(composeURL(latWest, lonSouth, latEast, lonNorth)).build();
Response response = client.newCall(request).execute();
if (response.code() == HTTPStatusCode.OK) {
return response.body().string();
} else if (response.code() == HTTPStatusCode.BAD_REQUEST) {
throw new HTTPException("Incorrect request to OSM server.");
} else {
throw new HTTPException("Inner OSM server error.");
}
} catch (IOException e) {
throw new HTTPException(e.getMessage());
}
}

private String composeURL(double latWest, double lonSouth, double latEast, double lonNorth) {
return serverAddress + downloadGeoDataURL + latWest + "," + lonSouth + "," + latEast + "," + lonNorth;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limita
*/

package ru.servers.resourceserver.network;

public class HTTPException extends Exception {

public HTTPException(String message) {
super(message);
}

public String getMessage(){
return super.getMessage();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limita
*/

package ru.servers.resourceserver.network;

public class HTTPStatusCode {

public static final short OK = 200;
public static final short BAD_REQUEST = 400;

}
4 changes: 4 additions & 0 deletions ServerSide/resourceserver/src/main/resources/osm.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Server API properties

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove empty line.

osm.serverAddress = http://www.openstreetmap.org
osm.url.downloadGeoData = /api/0.6/map?bbox=