Search in sources :

Example 1 with GPXTrackAnalysis

use of net.osmand.GPXUtilities.GPXTrackAnalysis 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 GPXTrackAnalysis

use of net.osmand.GPXUtilities.GPXTrackAnalysis in project OsmAnd-tools by osmandapp.

the class OsmGpxWriteContext method writeObf.

public File writeObf(List<File> files, File tmpFolder, String fileName, File targetObf) throws IOException, SQLException, InterruptedException, XmlPullParserException {
    startDocument();
    for (File gf : files) {
        GPXFile f = GPXUtilities.loadGPXFile(gf);
        GPXTrackAnalysis analysis = f.getAnalysis(gf.lastModified());
        writeTrack(null, null, f, analysis, "GPX");
    }
    endDocument();
    IndexCreatorSettings settings = new IndexCreatorSettings();
    settings.indexMap = true;
    settings.indexAddress = false;
    settings.indexPOI = true;
    settings.indexTransport = false;
    settings.indexRouting = false;
    RTree.clearCache();
    try {
        tmpFolder.mkdirs();
        IndexCreator ic = new IndexCreator(tmpFolder, settings);
        MapRenderingTypesEncoder types = new MapRenderingTypesEncoder(null, fileName);
        ic.setMapFileName(fileName);
        // IProgress.EMPTY_PROGRESS
        IProgress prog = IProgress.EMPTY_PROGRESS;
        // prog = new ConsoleProgressImplementation();
        ic.generateIndexes(qp.osmFile, prog, null, MapZooms.getDefault(), types, null);
        new File(tmpFolder, ic.getMapFileName()).renameTo(targetObf);
    } finally {
        Algorithms.removeAllFiles(tmpFolder);
    }
    return targetObf;
}
Also used : IndexCreatorSettings(net.osmand.obf.preparation.IndexCreatorSettings) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) IProgress(net.osmand.IProgress) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) IndexCreator(net.osmand.obf.preparation.IndexCreator) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXFile(net.osmand.GPXUtilities.GPXFile)

Example 3 with GPXTrackAnalysis

use of net.osmand.GPXUtilities.GPXTrackAnalysis in project OsmAnd-tools by osmandapp.

the class MapApiController method getSrtmGpx.

@GetMapping(path = { "/get-srtm-gpx-info" }, produces = "application/json")
@ResponseBody
public ResponseEntity<String> getSrtmGpx(HttpServletResponse response, HttpServletRequest request, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "updatetime", required = false) Long updatetime) throws IOException {
    PremiumUserDevice dev = checkUser();
    InputStream bin = null;
    try {
        @SuppressWarnings("unchecked") ResponseEntity<String>[] error = new ResponseEntity[] { null };
        UserFile userFile = userdataController.getUserFile(name, type, updatetime, dev);
        if (analysisPresent(SRTM_ANALYSIS, userFile)) {
            return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", userFile.details.get(SRTM_ANALYSIS))));
        }
        bin = userdataController.getInputStream(dev, error, userFile);
        ResponseEntity<String> err = error[0];
        if (err != null) {
            response.setStatus(err.getStatusCodeValue());
            response.getWriter().write(err.getBody());
            return err;
        }
        GPXFile gpxFile = GPXUtilities.loadGPXFile(new GZIPInputStream(bin));
        if (gpxFile == null) {
            return ResponseEntity.badRequest().body(String.format("File %s not found", userFile.name));
        }
        GPXFile srtmGpx = gpxController.calculateSrtmAltitude(gpxFile, null);
        GPXTrackAnalysis analysis = srtmGpx == null ? null : getAnalysis(userFile, srtmGpx);
        if (!analysisPresent(SRTM_ANALYSIS, userFile)) {
            saveAnalysis(SRTM_ANALYSIS, userFile, analysis);
        }
        return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", analysis)));
    } finally {
        if (bin != null) {
            bin.close();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ResponseEntity(org.springframework.http.ResponseEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with GPXTrackAnalysis

use of net.osmand.GPXUtilities.GPXTrackAnalysis in project OsmAnd-tools by osmandapp.

the class MapApiController method getGpxInfo.

@SuppressWarnings("unchecked")
@GetMapping(value = "/get-gpx-info")
@ResponseBody
public ResponseEntity<String> getGpxInfo(HttpServletResponse response, HttpServletRequest request, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "updatetime", required = false) Long updatetime) throws IOException, SQLException {
    PremiumUserDevice dev = checkUser();
    InputStream bin = null;
    try {
        ResponseEntity<String>[] error = new ResponseEntity[] { null };
        UserFile userFile = userdataController.getUserFile(name, type, updatetime, dev);
        if (analysisPresent(ANALYSIS, userFile)) {
            return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", userFile.details.get(ANALYSIS))));
        }
        bin = userdataController.getInputStream(dev, error, userFile);
        ResponseEntity<String> err = error[0];
        if (err != null) {
            response.setStatus(err.getStatusCodeValue());
            response.getWriter().write(err.getBody());
            return err;
        }
        GPXFile gpxFile = GPXUtilities.loadGPXFile(new GZIPInputStream(bin));
        if (gpxFile == null) {
            return ResponseEntity.badRequest().body(String.format("File %s not found", userFile.name));
        }
        GPXTrackAnalysis analysis = getAnalysis(userFile, gpxFile);
        if (!analysisPresent(ANALYSIS, userFile)) {
            saveAnalysis(ANALYSIS, userFile, analysis);
        }
        return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", analysis)));
    } finally {
        if (bin != null) {
            bin.close();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ResponseEntity(org.springframework.http.ResponseEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with GPXTrackAnalysis

use of net.osmand.GPXUtilities.GPXTrackAnalysis in project OsmAnd-tools by osmandapp.

the class MapApiController method getAnalysis.

private GPXTrackAnalysis getAnalysis(UserFile file, GPXFile gpxFile) {
    gpxFile.path = file.name;
    // file.clienttime == null ? 0 : file.clienttime.getTime()
    // keep 0
    GPXTrackAnalysis analysis = gpxFile.getAnalysis(0);
    gpxController.cleanupFromNan(analysis);
    return analysis;
}
Also used : GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis)

Aggregations

GPXTrackAnalysis (net.osmand.GPXUtilities.GPXTrackAnalysis)8 GPXFile (net.osmand.GPXUtilities.GPXFile)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)4 GZIPInputStream (java.util.zip.GZIPInputStream)3 PremiumUserDevice (net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice)3 UserFile (net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ResponseEntity (org.springframework.http.ResponseEntity)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Array (java.sql.Array)1 ResultSet (java.sql.ResultSet)1 Date (java.util.Date)1 IProgress (net.osmand.IProgress)1 OsmGpxWriteContext (net.osmand.obf.OsmGpxWriteContext)1 IndexCreator (net.osmand.obf.preparation.IndexCreator)1 IndexCreatorSettings (net.osmand.obf.preparation.IndexCreatorSettings)1 MapRenderingTypesEncoder (net.osmand.osm.MapRenderingTypesEncoder)1