Search in sources :

Example 11 with GPXFile

use of net.osmand.GPXUtilities.GPXFile 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 12 with GPXFile

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

the class MapApiController method listFiles.

@GetMapping(value = "/list-files")
@ResponseBody
public ResponseEntity<String> listFiles(@RequestParam(name = "name", required = false) String name, @RequestParam(name = "type", required = false) String type, @RequestParam(name = "allVersions", required = false, defaultValue = "false") boolean allVersions) throws IOException, SQLException {
    PremiumUserDevice dev = checkUser();
    if (dev == null) {
        return tokenNotValid();
    }
    UserFilesResults res = userdataController.generateFiles(dev.userid, name, type, allVersions, true);
    for (UserFileNoData nd : res.uniqueFiles) {
        String ext = nd.name.substring(nd.name.lastIndexOf('.') + 1);
        if (nd.type.equalsIgnoreCase("gpx") && ext.equalsIgnoreCase("gpx") && !analysisPresent(ANALYSIS, nd.details)) {
            GPXTrackAnalysis analysis = null;
            Optional<UserFile> of = userFilesRepository.findById(nd.id);
            UserFile uf = of.get();
            if (uf != null) {
                try {
                    InputStream in = uf.data != null ? new ByteArrayInputStream(uf.data) : userdataController.getInputStream(uf);
                    if (in != null) {
                        GPXFile gpxFile = GPXUtilities.loadGPXFile(new GZIPInputStream(in));
                        if (gpxFile != null) {
                            analysis = getAnalysis(uf, gpxFile);
                        }
                    }
                } catch (RuntimeException e) {
                }
                saveAnalysis(ANALYSIS, uf, analysis);
                nd.details = uf.details.deepCopy();
            }
        }
        if (analysisPresent(ANALYSIS, nd.details)) {
            nd.details.get(ANALYSIS).getAsJsonObject().remove("speedData");
            nd.details.get(ANALYSIS).getAsJsonObject().remove("elevationData");
        }
        if (analysisPresent(SRTM_ANALYSIS, nd.details)) {
            nd.details.get(SRTM_ANALYSIS).getAsJsonObject().remove("speedData");
            nd.details.get(SRTM_ANALYSIS).getAsJsonObject().remove("elevationData");
        }
    }
    return ResponseEntity.ok(gson.toJson(res));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) UserFilesResults(net.osmand.server.controllers.pub.UserdataController.UserFilesResults) ByteArrayInputStream(java.io.ByteArrayInputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) UserFileNoData(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFileNoData) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 13 with GPXFile

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

the class MapPanelSelector method drawGpxFiles.

private void drawGpxFiles(Collection<GPXFile> files) {
    DataTileManager<Entity> points = new DataTileManager<>(4);
    List<Way> ways = new ArrayList<>();
    for (GPXFile file : files) {
        for (Track track : file.tracks) {
            for (TrkSegment segment : track.segments) {
                Way w = new Way(-1);
                for (WptPt point : segment.points) {
                    w.addNode(new Node(point.lat, point.lon, -1));
                }
                ways.add(w);
            }
        }
        for (Way w : ways) {
            LatLon n = w.getLatLon();
            points.registerObject(n.getLatitude(), n.getLongitude(), w);
        }
        panel.setPoints(points);
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) WptPt(net.osmand.GPXUtilities.WptPt) LatLon(net.osmand.data.LatLon) DataTileManager(net.osmand.data.DataTileManager) Node(net.osmand.osm.edit.Node) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TrkSegment(net.osmand.GPXUtilities.TrkSegment) GPXFile(net.osmand.GPXUtilities.GPXFile) Way(net.osmand.osm.edit.Way) Track(net.osmand.GPXUtilities.Track)

Example 14 with GPXFile

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

the class WikivoyageGenOSM method combineAndSave.

private static boolean combineAndSave(CombinedWikivoyageArticle article, XmlSerializer serializer) throws IOException {
    article.updateCategoryCounts();
    long idStart = NODE_ID;
    LatLon mainArticlePoint = article.latlons.get(0);
    List<WptPt> points = new ArrayList<GPXUtilities.WptPt>();
    for (int i = 0; i < article.size(); i++) {
        String lng = article.langs.get(i);
        GPXFile file = article.points.get(i);
        String titleId = article.titles.get(i);
        if (mainArticlePoint == null) {
            mainArticlePoint = article.latlons.get(i);
        }
        for (WptPt p : file.getPoints()) {
            if (p.lat >= 90 || p.lat <= -90 || p.lon >= 180 || p.lon <= -180) {
                continue;
            }
            p.getExtensionsToWrite().put(LANG, lng);
            p.getExtensionsToWrite().put(TITLE, titleId);
            points.add(p);
            if (mainArticlePoint == null) {
                mainArticlePoint = new LatLon(p.getLatitude(), p.getLongitude());
            }
        }
    }
    if (mainArticlePoint == null) {
        // System.out.println(String.format("Skip article as it has no points: %s", article.titles));
        return false;
    }
    points = sortPoints(mainArticlePoint, points);
    serializer.startTag(null, "node");
    long mainArticleid = NODE_ID--;
    serializer.attribute(null, "id", mainArticleid + "");
    serializer.attribute(null, "action", "modify");
    serializer.attribute(null, "version", "1");
    serializer.attribute(null, "lat", latLonFormat.format(mainArticlePoint.getLatitude()));
    serializer.attribute(null, "lon", latLonFormat.format(mainArticlePoint.getLongitude()));
    tagValue(serializer, "route", "point");
    tagValue(serializer, "route_type", "article");
    addArticleTags(article, serializer, true);
    serializer.endTag(null, "node");
    for (WptPt p : points) {
        String category = simplifyWptCategory(p.category, CAT_OTHER);
        serializer.startTag(null, "node");
        long id = NODE_ID--;
        serializer.attribute(null, "id", id + "");
        serializer.attribute(null, "action", "modify");
        serializer.attribute(null, "version", "1");
        serializer.attribute(null, "lat", latLonFormat.format(p.lat));
        serializer.attribute(null, "lon", latLonFormat.format(p.lon));
        tagValue(serializer, "route", "point");
        tagValue(serializer, "route_type", "article_point");
        tagValue(serializer, "category", category);
        String lng = p.getExtensionsToRead().get(LANG);
        tagValue(serializer, "lang:" + lng, "yes");
        // addPointTags(article, serializer, p, ":" + lng);
        addPointTags(article, serializer, p, "");
        tagValue(serializer, "route_source", "wikivoyage");
        tagValue(serializer, "route_id", "Q" + article.tripId);
        serializer.endTag(null, "node");
    }
    long idEnd = NODE_ID;
    serializer.startTag(null, "way");
    long wayId = NODE_ID--;
    serializer.attribute(null, "id", wayId + "");
    serializer.attribute(null, "action", "modify");
    serializer.attribute(null, "version", "1");
    tagValue(serializer, "route", "points_collection");
    tagValue(serializer, "route_type", "article_points");
    addArticleTags(article, serializer, false);
    for (long nid = idStart; nid > idEnd; nid--) {
        serializer.startTag(null, "nd");
        serializer.attribute(null, "ref", nid + "");
        serializer.endTag(null, "nd");
    }
    serializer.endTag(null, "way");
    return true;
}
Also used : LatLon(net.osmand.data.LatLon) WptPt(net.osmand.GPXUtilities.WptPt) ArrayList(java.util.ArrayList) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXUtilities(net.osmand.GPXUtilities)

Example 15 with GPXFile

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

the class GpxController method calculateSrtmAltitude.

public GPXFile calculateSrtmAltitude(GPXFile gpxFile, File[] missingFile) {
    if (srtmLocation == null) {
        return null;
    }
    if (srtmLocation.startsWith("http://") || srtmLocation.startsWith("https://")) {
        String serverUrl = srtmLocation + "/gpx/process-srtm";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GPXUtilities.writeGpx(new OutputStreamWriter(baos), gpxFile, null);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
        ContentDisposition contentDisposition = ContentDisposition.builder("form-data").name("file").filename("route.gpx").build();
        fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
        HttpEntity<byte[]> fileEntity = new HttpEntity<>(baos.toByteArray(), fileMap);
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", fileEntity);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<byte[]> response = restTemplate.postForEntity(serverUrl, requestEntity, byte[].class);
        if (response.getStatusCode().is2xxSuccessful()) {
            return GPXUtilities.loadGPXFile(new ByteArrayInputStream(response.getBody()));
        } else {
            return null;
        }
    } else {
        File srtmFolder = new File(srtmLocation);
        if (!srtmFolder.exists()) {
            return null;
        }
        IndexHeightData hd = new IndexHeightData();
        hd.setSrtmData(srtmFolder);
        for (Track tr : gpxFile.tracks) {
            for (TrkSegment s : tr.segments) {
                for (int i = 0; i < s.points.size(); i++) {
                    WptPt wpt = s.points.get(i);
                    double h = hd.getPointHeight(wpt.lat, wpt.lon, missingFile);
                    if (h != IndexHeightData.INEXISTENT_HEIGHT) {
                        wpt.ele = h;
                    } else if (i == 0) {
                        return null;
                    }
                }
            }
        }
    }
    return gpxFile;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WptPt(net.osmand.GPXUtilities.WptPt) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrkSegment(net.osmand.GPXUtilities.TrkSegment) ContentDisposition(org.springframework.http.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) IndexHeightData(net.osmand.obf.preparation.IndexHeightData) RestTemplate(org.springframework.web.client.RestTemplate) OutputStreamWriter(java.io.OutputStreamWriter) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Track(net.osmand.GPXUtilities.Track)

Aggregations

GPXFile (net.osmand.GPXUtilities.GPXFile)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)7 GPXTrackAnalysis (net.osmand.GPXUtilities.GPXTrackAnalysis)7 WptPt (net.osmand.GPXUtilities.WptPt)7 InputStream (java.io.InputStream)6 TrkSegment (net.osmand.GPXUtilities.TrkSegment)6 File (java.io.File)5 IOException (java.io.IOException)5 LatLon (net.osmand.data.LatLon)5 GZIPInputStream (java.util.zip.GZIPInputStream)4 Track (net.osmand.GPXUtilities.Track)4 FileOutputStream (java.io.FileOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ResultSet (java.sql.ResultSet)3 ResponseEntity (org.springframework.http.ResponseEntity)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 Gson (com.google.gson.Gson)2 Point (java.awt.Point)2