-
Notifications
You must be signed in to change notification settings - Fork 982
/
TileFetcher.java
196 lines (163 loc) · 6.72 KB
/
TileFetcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.osmdroid.server.jdk;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
/**
* This is a REST web service (via Apache CXF) that provides 3 functions
* <ul><li>getSourceList() - provides a list of human readable tile sources</li>
* <li>getImage - gets a ZXY OSM style map tile</li>
* <li>serves from file system, a simple open layers slippy map that makes it easy to test to see if this thing is working
* and let's you flip tiles sources using some jquery magic</li>
* </ul>
*
* @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
*/
@Path("/")
@Produces({"image/png", "application/json", "text/html", "text/css", "text/javascript"})
@org.apache.cxf.jaxrs.model.wadl.Description("")
public class TileFetcher {
private static final Log log = LogFactory.getLog(TileFetcher.class);
static ObjectMapper om = new ObjectMapper();
HashMap<String, Connection> connections = new HashMap<String, Connection>();
public TileFetcher() throws Exception {
initDatabases();
}
private void initDatabases() throws Exception {
Properties p = new Properties();
FileInputStream fis = new FileInputStream("sources.properties");
p.load(fis);
fis.close();
fis = null;
Iterator<Map.Entry<Object, Object>> iterator = p.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Object, Object> next = iterator.next();
String source = (String) next.getKey();
String filename = (String) next.getValue();
File db = new File(filename);
if (!db.exists()) {
throw new FileNotFoundException("can't find the db " + filename + " current dir is " + new File(".").getAbsolutePath());
}
try {
Connection conn1 = DriverManager.getConnection("jdbc:sqlite:" + filename);
Statement stat = conn1.createStatement();
stat.executeUpdate("CREATE TABLE IF NOT EXISTS tiles (key INTEGER PRIMARY KEY, provider TEXT, tile BLOB)");
stat.close();
System.out.println("adding " + source + " from file " + filename);
connections.put(source, conn1);
} catch (SQLException e) {
e.printStackTrace();
//throw new Exception("unable to initialize db", e);
}
}
}
@GET
@Path("/sources")
@Produces("application/json")
@org.apache.cxf.jaxrs.model.wadl.Description("Returns a JSON string array of all available map sources")
public String getSourceList() throws WebApplicationException, JsonProcessingException {
System.out.println("getSourceList");
return om.writeValueAsString(connections.keySet());
}
@GET//xyz no good
//zyx no good
//zxy closer
@Path("/{source}/{z}/{x}/{y}.png")
@Produces("image/png")
@org.apache.cxf.jaxrs.model.wadl.Description("Returns png of the specific map tile from the database")
public byte[] getImage(@PathParam("source") String id,
@PathParam("z") int z,
@PathParam("x") int x,
@PathParam("y") int y) throws WebApplicationException {
Connection c = connections.get(id);
if (c == null) {
System.err.println(id + " isn't registered");
throw new WebApplicationException(new Exception(id + " is not a valid tile source"), 400);
}
try {
PreparedStatement prep = c.prepareStatement("Select tile from tiles where key=?;");
long index = (((z << z) + x) << z) + y;
System.out.println("Fetching tile " + id + z + "/" + x + "/" + y + " as " + index);
prep.setLong(1, index);
ResultSet executeQuery = prep.executeQuery();
if (executeQuery.next()) {
//Blob b= executeQuery.getBlob(1);
//byte[] image=b.getBytes(0, (int)b.length());
byte[] image2 = executeQuery.getBytes(1);
//return image;
return image2;
}
System.out.println(id + "Tile not found " + z + "/" + x + "/" + y + " as " + index);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
throw new WebApplicationException(404);
}
@GET
@Path("/index.html")
@Produces("text/html")
@org.apache.cxf.jaxrs.model.wadl.Description("Returns a basic html viewer of the slippy map")
public String getIndex() throws WebApplicationException {
return getFile("www/openlayers.html");
}
@GET
@Path("/")
@Produces("text/html")
@org.apache.cxf.jaxrs.model.wadl.Description("Returns a basic html viewer of the slippy map")
public String getIndex4() throws WebApplicationException {
return getFile("www/openlayers.html");
}
@GET
@Path("/v3.5.0-dist/ol.js")
@Produces("application/javascript")
@org.apache.cxf.jaxrs.model.wadl.Description("Returns a basic html viewer of the slippy map")
public String getIndex2() throws WebApplicationException {
return getFile("www/v3.5.0-dist/ol.js");
}
@GET
@Path("/v3.5.0-dist/ol.css")
@Produces("text/css")
@org.apache.cxf.jaxrs.model.wadl.Description("Returns a basic html viewer of the slippy map")
public String getIndex3() throws WebApplicationException {
return getFile("www/v3.5.0-dist/ol.css");
}
public static String getFile(String f) {
StringBuilder sb = new StringBuilder();
try {
FileInputStream r = new FileInputStream(f);
int c = 0;
while ((c = r.read()) != -1) {
sb.append((char) c);
}
r.close();
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("Current dir is " + new File(".").getAbsolutePath());
}
return sb.toString();
}
}