001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.io.imagery;
003
004 import static org.openstreetmap.josm.tools.I18n.tr;
005
006 import java.io.BufferedReader;
007 import java.io.InputStreamReader;
008 import java.net.URL;
009 import java.net.URLEncoder;
010
011 import org.openstreetmap.josm.Main;
012 import org.openstreetmap.josm.data.coor.EastNorth;
013 import org.openstreetmap.josm.data.coor.LatLon;
014 import org.openstreetmap.josm.data.imagery.ImageryInfo;
015 import org.openstreetmap.josm.data.preferences.StringProperty;
016
017 public class OsmosnimkiOffsetServer implements OffsetServer {
018 public static final StringProperty PROP_SERVER_URL = new StringProperty("imagery.offsetserver.url","http://offset.osmosnimki.ru/offset/v0?");
019 private String url;
020
021 public OsmosnimkiOffsetServer(String url) {
022 this.url = url;
023 }
024
025 @Override
026 public boolean isLayerSupported(ImageryInfo info) {
027 try {
028 URL url = new URL(this.url + "action=CheckAvailability&id=" + URLEncoder.encode(info.getUrl(), "UTF-8"));
029 System.out.println(tr("Querying offset availability: {0}", url));
030 final BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"));
031 String response = rdr.readLine();
032 System.out.println(tr("Offset server response: {0}", response));
033 if (response == null)
034 return false;
035 if (response.contains("\"offsets_available\": true")) return true;
036 } catch (Exception e) {
037 e.printStackTrace();
038 }
039 return false;
040 }
041
042 @Override
043 public EastNorth getOffset(ImageryInfo info, EastNorth en) {
044 LatLon ll = Main.getProjection().eastNorth2latlon(en);
045 try {
046 URL url = new URL(this.url + "action=GetOffsetForPoint&lat=" + ll.lat() + "&lon=" + ll.lon() + "&id=" + URLEncoder.encode(info.getUrl(), "UTF-8"));
047 System.out.println(tr("Querying offset: {0}", url.toString()));
048 final BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"));
049 String s = rdr.readLine();
050 if (s == null)
051 return null;
052 int i = s.indexOf(',');
053 if (i == -1) return null;
054 String sLon = s.substring(1,i);
055 String sLat = s.substring(i+1,s.length()-1);
056 return Main.getProjection().latlon2eastNorth(new LatLon(Double.valueOf(sLat),Double.valueOf(sLon))).sub(en);
057 } catch (Exception e) {
058 e.printStackTrace();
059 return null;
060 }
061 }
062
063 }