Search in sources :

Example 1 with NativeLibrary

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

the class OsmAndImageRendering method main.

public static void main(String[] args) throws IOException, XmlPullParserException, SAXException, ParserConfigurationException, TransformerException {
    String nativeLib = null;
    String eyepiece = null;
    String dirWithObf = null;
    String websiteUrl = "";
    String gpxFile = null;
    String outputFiles = null;
    String stylesPath = null;
    args = setupDefaultAttrs(args);
    for (String a : args) {
        if (a.startsWith("-native=")) {
            nativeLib = a.substring("-native=".length());
        } else if (a.startsWith("-websiteUrl=")) {
            // https://jenkins.osmand.net/job/OsmAndTestRendering/ws/rendering-tests/
            websiteUrl = a.substring("-websiteUrl=".length());
        } else if (a.startsWith("-obfFiles=")) {
            dirWithObf = a.substring("-obfFiles=".length());
        } else if (a.startsWith("-gpxFile=")) {
            gpxFile = a.substring("-gpxFile=".length());
        } else if (a.startsWith("-output=")) {
            outputFiles = a.substring("-output=".length());
        } else if (a.startsWith("-eyepiece=")) {
            eyepiece = a.substring("-eyepiece=".length());
        } else if (a.startsWith("-stylesPath=")) {
            stylesPath = a.substring("-stylesPath=".length());
        }
    }
    String backup = null;
    if (nativeLib != null) {
        boolean old = NativeLibrary.loadOldLib(nativeLib);
        NativeLibrary nl = new NativeLibrary();
        nl.loadFontData(new File("fonts"));
        if (!old) {
            throw new UnsupportedOperationException("Not supported");
        }
    }
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = docBuilder.parse(new File(gpxFile));
    Element de = doc.getDocumentElement();
    String defZoom = getAttribute(de, "zoom", "11");
    String defMapDensity = getAttribute(de, "mapDensity", "1");
    String defRenderingName = getAttribute(de, "renderingName", "default");
    String defRenderingProps = getAttribute(de, "renderingProperties", "");
    String defWidth = getAttribute(de, "width", "1366");
    String defHeight = getAttribute(de, "height", "768");
    NodeList nl = doc.getElementsByTagName("wpt");
    NodeList gen = doc.getElementsByTagName("html");
    File gpx = new File(gpxFile);
    HTMLContent html = null;
    if (gen.getLength() > 0) {
        String gpxName = gpx.getName().substring(0, gpx.getName().length() - 4);
        String outputFName = gpxName;
        if (eyepiece != null) {
        // outputFName += "_eye";
        }
        html = new HTMLContent(new File(gpx.getParentFile(), outputFName + ".html"), websiteUrl + gpxName);
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(gen.item(0)), new StreamResult(sw));
        html.setContent(sw.toString());
    }
    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        double lat = Double.parseDouble(e.getAttribute("lat"));
        double lon = Double.parseDouble(e.getAttribute("lon"));
        int imageHeight = Integer.parseInt(getSubAttr(e, "height", defHeight));
        int imageWidth = Integer.parseInt(getSubAttr(e, "width", defWidth));
        String name = getSubAttr(e, "name", (lat + "!" + lon).replace('.', '_'));
        String maps = getSubAttr(e, "maps", "");
        String mapDensities = getSubAttr(e, "mapDensity", defMapDensity);
        String zooms = getSubAttr(e, "zoom", defZoom);
        String renderingNames = getSubAttr(e, "renderingName", defRenderingName);
        String renderingProperties = getSubAttr(e, "renderingProperties", defRenderingProps);
        if (maps.isEmpty()) {
            throw new UnsupportedOperationException("No maps element found for wpt " + name);
        }
        NativeSwingRendering nsr = nativeLib != null ? new NativeSwingRendering() : null;
        // nsr.initFilesInDir(new File(dirWithObf));
        ArrayList<File> obfFiles = new ArrayList<File>();
        initMaps(dirWithObf, backup, gpxFile, maps, nsr, obfFiles);
        List<ImageCombination> ls = getCombinations(name, zooms, mapDensities, renderingNames, renderingProperties);
        if (html != null) {
            html.newRow(getSubAttr(e, "desc", ""));
        }
        for (ImageCombination ic : ls) {
            System.out.println("Generate " + ic.generateName + " style " + ic.renderingStyle);
            if (eyepiece != null) {
                try {
                    String line = eyepiece;
                    double dx = ic.mapDensity;
                    line += " -latLon=" + lat + ";" + lon;
                    line += " -stylesPath=\"" + stylesPath + "\"";
                    String styleName = ic.renderingStyle;
                    if (styleName.endsWith(".render.xml")) {
                        styleName = styleName.substring(0, styleName.length() - ".render.xml".length());
                    }
                    line += " -styleName=" + styleName;
                    for (File f : obfFiles) {
                        line += " -obfFile=\"" + f.getAbsolutePath() + "\"";
                    }
                    String[] listProps = ic.renderingProperties.split(",");
                    for (String p : listProps) {
                        if (p.trim().length() > 0) {
                            line += " -styleSetting:" + p;
                        }
                    }
                    // line += " -useLegacyContext";
                    line += " -outputImageWidth=" + imageWidth;
                    line += " -outputImageHeight=" + imageHeight;
                    final String fileName = ic.generateName + ".png";
                    line += " -outputImageFilename=\"" + outputFiles + "/" + fileName + "\"";
                    line += " -referenceTileSize=" + (int) (dx * 256);
                    line += " -displayDensityFactor=" + dx;
                    line += " -zoom=" + ic.zoom;
                    line += " -outputImageFormat=png";
                    line += " -verbose";
                    System.out.println(line);
                    Process p = Runtime.getRuntime().exec(line);
                    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                    input.close();
                    if (html != null) {
                        html.addFile(fileName);
                    }
                } catch (Exception err) {
                    err.printStackTrace();
                }
            }
            if (nativeLib != null) {
                nsr.loadRuleStorage(ic.renderingStyle, ic.renderingProperties);
                BufferedImage mg = nsr.renderImage(new RenderingImageContext(lat, lon, imageWidth, imageHeight, ic.zoom, ic.mapDensity));
                ImageWriter writer = ImageIO.getImageWritersBySuffix("png").next();
                final String fileName = ic.generateName + ".png";
                if (html != null) {
                    html.addFile(fileName);
                }
                writer.setOutput(new FileImageOutputStream(new File(outputFiles, fileName)));
                writer.write(mg);
            }
        }
    }
    if (html != null) {
        html.write();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ImageWriter(javax.imageio.ImageWriter) BufferedImage(java.awt.image.BufferedImage) RenderingImageContext(net.osmand.swing.NativeSwingRendering.RenderingImageContext) StringWriter(java.io.StringWriter) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) NodeList(org.w3c.dom.NodeList) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BufferedReader(java.io.BufferedReader) NativeLibrary(net.osmand.NativeLibrary) File(java.io.File)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 ImageWriter (javax.imageio.ImageWriter)1 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 NativeLibrary (net.osmand.NativeLibrary)1 RenderingImageContext (net.osmand.swing.NativeSwingRendering.RenderingImageContext)1 Element (org.w3c.dom.Element)1