use of net.osmand.obf.OsmGpxWriteContext in project OsmAnd-tools by osmandapp.
the class DownloadOsmGPX method queryGPXForBBOX.
protected void queryGPXForBBOX(QueryParams qp) throws SQLException, IOException, FactoryConfigurationError, XMLStreamException, InterruptedException, XmlPullParserException {
String conditions = "";
if (!Algorithms.isEmpty(qp.user)) {
conditions += " and t.\"user\" = '" + qp.user + "'";
}
if (!Algorithms.isEmpty(qp.tag)) {
// conditions += " and '" + qp.tag + "' = ANY(t.tags)";
String[] tagsAnd = qp.tag.split(",");
for (String tagAnd : tagsAnd) {
conditions += " and (";
String[] tagsOr = tagAnd.split("\\;");
boolean t = false;
for (String tagOr : tagsOr) {
if (t) {
conditions += " or ";
}
conditions += " lower('^'||array_to_string(t.tags,'^','')||'^') like '%^" + tagOr.trim().toLowerCase() + "^%'";
t = true;
}
conditions += ")";
}
}
if (!Algorithms.isEmpty(qp.datestart)) {
conditions += " and t.date >= '" + qp.datestart + "'";
}
if (!Algorithms.isEmpty(qp.dateend)) {
conditions += " and t.date <= '" + qp.dateend + "'";
}
if (qp.minlat != OsmGpxFile.ERROR_NUMBER) {
conditions += " and t.maxlat >= " + qp.minlat;
conditions += " and t.minlat <= " + qp.maxlat;
conditions += " and t.maxlon >= " + qp.minlon;
conditions += " and t.minlon <= " + qp.maxlon;
}
String query = "SELECT t.id, s.data, t.name, t.description, t.\"user\", t.date, t.tags from " + GPX_METADATA_TABLE_NAME + " t join " + GPX_FILES_TABLE_NAME + " s on s.id = t.id " + " where 1 = 1 " + conditions + " order by t.date asc";
if (qp.limit != -1) {
query += " limit " + qp.limit;
}
System.out.println(query);
ResultSet rs = dbConn.createStatement().executeQuery(query);
OsmGpxWriteContext ctx = new OsmGpxWriteContext(qp);
ctx.startDocument();
Date lastTimestamp = null;
while (rs.next()) {
if ((ctx.tracks + 1) % 1000 == 0) {
System.out.println(String.format("Fetched %d tracks %d segments - last %s (now %s)", ctx.tracks + 1, ctx.segments, lastTimestamp, new Date()));
}
OsmGpxFile gpxInfo = new OsmGpxFile();
gpxInfo.id = rs.getLong(1);
byte[] cont = rs.getBytes(2);
if (cont == null) {
continue;
}
gpxInfo.name = rs.getString(3);
gpxInfo.description = rs.getString(4);
gpxInfo.user = rs.getString(5);
gpxInfo.timestamp = new Date(rs.getDate(6).getTime());
lastTimestamp = gpxInfo.timestamp;
Array tags = rs.getArray(7);
List<String> trackTags = new ArrayList<>();
if (tags != null) {
ResultSet rsar = tags.getResultSet();
while (rsar.next()) {
String tg = rsar.getString(2);
if (tg != null) {
trackTags.add(tg.toLowerCase());
}
}
}
gpxInfo.tags = trackTags.toArray(new String[0]);
if (qp.activityTypes != null) {
RouteActivityType rat = RouteActivityType.getTypeFromTags(gpxInfo.tags);
if (rat == null || !qp.activityTypes.contains(rat)) {
continue;
}
}
ByteArrayInputStream is = new ByteArrayInputStream(Algorithms.gzipToString(cont).getBytes());
GPXFile gpxFile = GPXUtilities.loadGPXFile(is);
GPXTrackAnalysis analysis = gpxFile.getAnalysis(gpxInfo.timestamp.getTime());
ctx.writeTrack(gpxInfo, null, gpxFile, analysis, "OG");
}
ctx.endDocument();
System.out.println(String.format("Fetched %d tracks %d segments", ctx.tracks, ctx.segments));
generateObfFile(qp);
}
use of net.osmand.obf.OsmGpxWriteContext in project OsmAnd-tools by osmandapp.
the class GpxController method downloadObf.
@RequestMapping(path = { "/download-obf" })
@ResponseBody
public ResponseEntity<Resource> downloadObf(@RequestParam(defaultValue = "", required = false) String gzip, HttpSession httpSession, HttpServletResponse resp) throws IOException, FactoryConfigurationError, XMLStreamException, SQLException, InterruptedException, XmlPullParserException {
GPXSessionContext ctx = session.getGpxResources(httpSession);
File tmpOsm = File.createTempFile("gpx_obf_" + httpSession.getId(), ".osm.gz");
ctx.tempFiles.add(tmpOsm);
List<File> files = new ArrayList<>();
for (GPXSessionFile f : ctx.files) {
files.add(f.file);
}
String sessionId = httpSession.getId();
File tmpFolder = new File(tmpOsm.getParentFile(), sessionId);
String fileName = "gpx_" + sessionId;
QueryParams qp = new QueryParams();
qp.osmFile = tmpOsm;
qp.details = QueryParams.DETAILS_ELE_SPEED;
OsmGpxWriteContext writeCtx = new OsmGpxWriteContext(qp);
File targetObf = new File(tmpFolder.getParentFile(), fileName + IndexConstants.BINARY_MAP_INDEX_EXT);
writeCtx.writeObf(files, tmpFolder, fileName, targetObf);
ctx.tempFiles.add(targetObf);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"gpx.obf\""));
headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-binary");
return ResponseEntity.ok().headers(headers).body(new FileSystemResource(targetObf));
}
Aggregations