Search in sources :

Example 1 with OsmGpxWriteContext

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);
}
Also used : GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) OsmGpxWriteContext(net.osmand.obf.OsmGpxWriteContext) Array(java.sql.Array) RouteActivityType(net.osmand.osm.RouteActivityType) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) GPXFile(net.osmand.GPXUtilities.GPXFile)

Example 2 with OsmGpxWriteContext

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));
}
Also used : GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) HttpHeaders(org.springframework.http.HttpHeaders) GPXSessionContext(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext) ArrayList(java.util.ArrayList) QueryParams(net.osmand.obf.OsmGpxWriteContext.QueryParams) FileSystemResource(org.springframework.core.io.FileSystemResource) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) OsmGpxWriteContext(net.osmand.obf.OsmGpxWriteContext) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

GPXFile (net.osmand.GPXUtilities.GPXFile)2 OsmGpxWriteContext (net.osmand.obf.OsmGpxWriteContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Array (java.sql.Array)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 GPXTrackAnalysis (net.osmand.GPXUtilities.GPXTrackAnalysis)1 QueryParams (net.osmand.obf.OsmGpxWriteContext.QueryParams)1 RouteActivityType (net.osmand.osm.RouteActivityType)1 GPXSessionContext (net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext)1 GPXSessionFile (net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 StreamingResponseBody (org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody)1