Search in sources :

Example 6 with GPXFile

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;
}
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 7 with GPXFile

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));
}
Also used : Statement(java.sql.Statement) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) GZIPInputStream(java.util.zip.GZIPInputStream) DBDialect(net.osmand.obf.preparation.DBDialect) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) GPXFile(net.osmand.GPXUtilities.GPXFile) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 8 with GPXFile

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);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) Autowired(org.springframework.beans.factory.annotation.Autowired) WptPt(net.osmand.GPXUtilities.WptPt) RequestPart(org.springframework.web.bind.annotation.RequestPart) NotEmpty(javax.validation.constraints.NotEmpty) Valid(javax.validation.Valid) ByteArrayInputStream(java.io.ByteArrayInputStream) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) Gson(com.google.gson.Gson) IndexConstants(net.osmand.IndexConstants) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) Speed(net.osmand.GPXUtilities.Speed) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) RestTemplate(org.springframework.web.client.RestTemplate) Resource(org.springframework.core.io.Resource) HttpSession(javax.servlet.http.HttpSession) PostMapping(org.springframework.web.bind.annotation.PostMapping) GPXFile(net.osmand.GPXUtilities.GPXFile) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) FileSystemResource(org.springframework.core.io.FileSystemResource) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) NotNull(javax.validation.constraints.NotNull) QueryParams(net.osmand.obf.OsmGpxWriteContext.QueryParams) RestController(org.springframework.web.bind.annotation.RestController) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) OsmAndProUser(net.osmand.server.WebSecurityConfiguration.OsmAndProUser) IndexHeightData(net.osmand.obf.preparation.IndexHeightData) Elevation(net.osmand.GPXUtilities.Elevation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) GPXUtilities(net.osmand.GPXUtilities) TrkSegment(net.osmand.GPXUtilities.TrkSegment) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) IProgress(net.osmand.IProgress) SQLException(java.sql.SQLException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentDisposition(org.springframework.http.ContentDisposition) OutputStreamWriter(java.io.OutputStreamWriter) GetMapping(org.springframework.web.bind.annotation.GetMapping) Algorithms(net.osmand.util.Algorithms) Track(net.osmand.GPXUtilities.Track) Iterator(java.util.Iterator) GPXSessionContext(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) FileOutputStream(java.io.FileOutputStream) MultiValueMap(org.springframework.util.MultiValueMap) OsmGpxWriteContext(net.osmand.obf.OsmGpxWriteContext) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) File(java.io.File) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) InputStream(java.io.InputStream) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) OutputStreamWriter(java.io.OutputStreamWriter) GPXFile(net.osmand.GPXUtilities.GPXFile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 9 with GPXFile

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()]))));
    }
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeMap(java.util.TreeMap) LatLon(net.osmand.data.LatLon) GPXFile(net.osmand.GPXUtilities.GPXFile) RouteSegmentResult(net.osmand.router.RouteSegmentResult) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 10 with GPXFile

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

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