use of net.osmand.GPXUtilities.GPXFile 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;
}
use of net.osmand.GPXUtilities.GPXFile in project OsmAnd-tools by osmandapp.
the class WikivoyageGenOSM method genWikivoyageOsm.
public static void genWikivoyageOsm(File wikivoyageFile, File outputFile, int LIMIT) throws SQLException, IOException {
DBDialect dialect = DBDialect.SQLITE;
Connection connection = (Connection) dialect.getDatabaseConnection(wikivoyageFile.getCanonicalPath(), log);
Statement statement = connection.createStatement();
// popular_articles : trip_id, popularity_index, order_index, population, title, lat, lon, lang
// travel_articles:
// population, country, region, city_type, osm_i,
ResultSet rs = statement.executeQuery("select trip_id, title, lang, lat, lon, content_gz, " + "gpx_gz, image_title, banner_title, is_part_of, is_parent_of, aggregated_part_of, contents_json from travel_articles order by trip_id asc");
int count = 0, totalArticles = 0, emptyLocation = 0, emptyContent = 0;
CombinedWikivoyageArticle combinedArticle = new CombinedWikivoyageArticle();
XmlSerializer serializer = null;
OutputStream outputStream = null;
if (outputFile != null) {
outputStream = new FileOutputStream(outputFile);
if (outputFile.getName().endsWith(".gz")) {
outputStream = new GZIPOutputStream(outputStream);
}
serializer = PlatformUtil.newSerializer();
serializer.setOutput(new OutputStreamWriter(outputStream));
// $NON-NLS-1$
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
// $NON-NLS-1$
serializer.startDocument("UTF-8", true);
// $NON-NLS-1$
serializer.startTag(null, "osm");
// $NON-NLS-1$ //$NON-NLS-2$
serializer.attribute(null, "version", "0.6");
}
while (rs.next()) {
int rind = 1;
long tripId = rs.getLong(rind++);
if (tripId != combinedArticle.tripId && combinedArticle.tripId != -1) {
boolean res = combineAndSave(combinedArticle, serializer);
if (res) {
totalArticles++;
}
combinedArticle.clear();
}
combinedArticle.tripId = tripId;
String title = rs.getString(rind++);
String lang = rs.getString(rind++);
double lat = rs.getDouble(rind++);
double lon = rs.getDouble(rind++);
// rind++;
String content = Algorithms.gzipToString(rs.getBytes(rind++));
GZIPInputStream bytesStream = new GZIPInputStream(new ByteArrayInputStream(rs.getBytes(rind++)));
GPXFile gpxFile = GPXUtilities.loadGPXFile(bytesStream);
String imageTitle = rs.getString(rind++);
String bannerTitle = rs.getString(rind++);
String isPartOf = rs.getString(rind++);
String isParentOf = rs.getString(rind++);
String isAggrPartOf = rs.getString(rind++);
String contentJson = rs.getString(rind);
combinedArticle.addArticle(lang, title, gpxFile, lat, lon, content, imageTitle, bannerTitle, isPartOf, isParentOf, isAggrPartOf, contentJson);
if (gpxFile == null || gpxFile.isPointsEmpty()) {
if (lat == 0 && lon == 0) {
emptyLocation++;
} else {
emptyContent++;
}
}
if (count >= LIMIT && LIMIT != -1) {
break;
}
count++;
}
combineAndSave(combinedArticle, serializer);
if (serializer != null) {
serializer.endTag(null, "osm");
serializer.flush();
outputStream.close();
}
List<String> l = new ArrayList<String>(categories.keySet());
Collections.sort(l, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return Integer.compare(categories.get(s1), categories.get(s2));
}
});
int total = 0;
for (String s : l) {
total += categories.get(s);
}
for (String s : l) {
int cnt = categories.get(s);
System.out.println(String.format("%#.2f%% %s %d %s", cnt * 100.0 / total, s, cnt, categoriesExample.get(s)));
}
System.out.println(String.format("Total saved articles: %d", totalArticles));
System.out.println(String.format("Empty article: %d no points in article + %d no location page articles (total %d) ", emptyContent, emptyLocation, total));
}
use of net.osmand.GPXUtilities.GPXFile in project OsmAnd-tools by osmandapp.
the class GpxController method attachSrtm.
@PostMapping(path = { "/process-srtm" }, produces = "application/json")
public ResponseEntity<StreamingResponseBody> attachSrtm(@RequestPart(name = "file") @Valid @NotNull @NotEmpty MultipartFile file) throws IOException {
GPXFile gpxFile = GPXUtilities.loadGPXFile(file.getInputStream());
GPXFile srtmGpx = calculateSrtmAltitude(gpxFile, null);
StreamingResponseBody responseBody = outputStream -> {
GPXUtilities.writeGpx(new OutputStreamWriter(outputStream), srtmGpx, IProgress.EMPTY_PROGRESS);
};
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName()).contentType(MediaType.APPLICATION_XML).body(responseBody);
}
use of net.osmand.GPXUtilities.GPXFile in project OsmAnd-tools by osmandapp.
the class RoutingController method uploadGpx.
@PostMapping(path = { "/gpx-approximate" }, produces = "application/json")
public ResponseEntity<String> uploadGpx(@RequestPart(name = "file") @Valid @NotNull @NotEmpty MultipartFile file, @RequestParam(defaultValue = "car") String routeMode) throws IOException {
InputStream is = file.getInputStream();
GPXFile gpxFile = GPXUtilities.loadGPXFile(is);
is.close();
if (gpxFile.error != null) {
return ResponseEntity.badRequest().body("Error reading gpx!");
} else {
gpxFile.path = file.getOriginalFilename();
List<LatLon> resList = new ArrayList<LatLon>();
List<Feature> features = new ArrayList<Feature>();
Map<String, Object> props = new TreeMap<>();
try {
List<RouteSegmentResult> res = osmAndMapsService.gpxApproximation(routeMode, props, gpxFile);
convertResults(resList, features, res);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
} catch (RuntimeException e) {
LOGGER.error(e.getMessage(), e);
}
Feature route = new Feature(Geometry.lineString(resList));
features.add(0, route);
route.properties = props;
return ResponseEntity.ok(gson.toJson(new FeatureCollection(features.toArray(new Feature[features.size()]))));
}
}
use of net.osmand.GPXUtilities.GPXFile 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();
}
}
}
Aggregations