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();
}
}
}
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));
}
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);
}
}
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;
}
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;
}
Aggregations