How to convert world coordinates to screen coordinates (pixels)

المشرف العام

Administrator
طاقم الإدارة
I am trying to export and display an SVG file, the exporting works, I even added some custom grouping. However the image is either not displayed at all or not displayed properly (very small and lines are filled in). I feel like the main problem is I am exporting the world coordinates of my map and are not valid points for the svg file. I do have a file that displays correctly (same map), however is auto generated and I do not know if I can customize the output.

Code:

protected void exportToSVG2(File file, JMapFrame jframe, MapContent map, FeatureCollection collection) { ArrayList featureList = new ArrayList(); String id = "id"; try (FeatureIterator iterator = collection.features()) { while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); featureList.add(feature); } } catch (Exception ex) { Logger.getLogger(ShapefileExporter.class.getName()).log(Level.SEVERE, null, ex); } try { Dimension canvasSize = new Dimension(jframe.getMapPane().getSize()); int minX = (int)map.getViewport().getScreenArea().getMinX(); int minY = (int)map.getViewport().getScreenArea().getMinY(); int width = (int)map.getViewport().getScreenArea().getWidth()/2; int height = (int)map.getViewport().getScreenArea().getHeight()/2; String viewBox = Integer.toString(minX) + " " + Integer.toString(minY) + " " + Integer.toString(width) + " " + Integer.toString(height); Rectangle paintArea = new Rectangle(canvasSize); AffineTransform aff = RendererUtilities.worldToScreenTransform(map.getMaxBounds(), paintArea); System.out.println("AffineTrans" + aff);//Output: AffineTransAffineTransform[[456.4886205260367, 0.0, 33488.61724598585], [0.0, -292.6885824158927, 13176.22251995589]] // Create SVG document String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); Document doc = impl.createDocument(svgNS, "svg", null); // Get the root element (the 'svg' element) Element svgRoot = doc.getDocumentElement(); // Set the width and heigh attributes on the root 'svg' element svgRoot.setAttributeNS(null, "width", Integer.toString(canvasSize.width) ); svgRoot.setAttributeNS(null, "height", Integer.toString(canvasSize.height) ); svgRoot.setAttributeNS(null, "viewBox", viewBox); System.out.println("Viewbox: " + viewBox); Element id115 = doc.createElementNS(svgNS, "g"); svgRoot.appendChild(id115); id115.setAttributeNS(svgNS, "id", "115"); Element id120 = doc.createElementNS(svgNS, "g"); svgRoot.appendChild(id120); id120.setAttributeNS(svgNS, "id", "120"); Element id230 = doc.createElementNS(svgNS, "g"); svgRoot.appendChild(id230); id230.setAttributeNS(svgNS, "id", "230"); Element id345 = doc.createElementNS(svgNS, "g"); svgRoot.appendChild(id345); id345.setAttributeNS(svgNS, "id", "345"); Element id450 = doc.createElementNS(svgNS, "g"); svgRoot.appendChild(id450); id450.setAttributeNS(svgNS, "id", "450"); Element line; // group by ID for (SimpleFeature feat : featureList) { feat.getAttribute(id).toString()); String featID = feat.getAttribute(id).toString(); String featCoordinates = feat.getAttribute(geom).toString().replaceAll("[^-., 0-9]", "").replaceFirst(" ", ""); if (featID.equals("115")) { // Line element line = doc.createElement("polyline"); line.setAttributeNS(svgNS, "points", featCoordinates ); id115.appendChild(line); } else if (featID.equals("120")) { // Line element line = doc.createElement("polyline"); line.setAttributeNS(svgNS, "points", featCoordinates ); id120.appendChild(line); } else if (featID.equals("230")) { // Line element line = doc.createElement("polyline"); line.setAttributeNS(svgNS, "points", featCoordinates ); id230.appendChild(line); } else if (featID.equals("345")) { // Line element line = doc.createElement("polyline"); line.setAttributeNS(svgNS, "points", featCoordinates ); id345.appendChild(line); } else if (featID.equals("450")) { // Line element line = doc.createElement("polyline"); line.setAttributeNS(svgNS, "points", featCoordinates ); id450.appendChild(line); } } // Set up the map SVGGeneratorContext ctx1 = SVGGeneratorContext.createDefault(doc); SVGGeneratorContext ctx = ctx1; ctx.setComment("Generated by GeoTools with Batik SVG Generator"); SVGGraphics2D g2d = new SVGGraphics2D(ctx, false); g2d.setSVGCanvasSize(canvasSize); Rectangle outputArea = new Rectangle(g2d.getSVGCanvasSize()); ReferencedEnvelope dataArea = map.getViewport().getBounds(); try { jframe.getMapPane().getRenderer().paint(g2d, outputArea, dataArea); } catch (NullPointerException ex) { System.err.println("NullPointerException found: " + ex); } OutputStreamWriter osw = null; //jframe.paintComponents(g2d); try { OutputStream out = new FileOutputStream(file); osw = null; osw = new OutputStreamWriter(out, "UTF-8"); //g2d.stream g2d.stream(svgRoot, osw, true); } catch (FileNotFoundException | UnsupportedEncodingException | SVGGraphics2DIOException ex) { System.err.println("IOException found: " + ex); } finally { if (osw != null) { try { osw.close(); } catch (IOException ex) { System.err.println("IOException found: " + ex); } } } // End inner try } catch (Exception ex) { Logger.getLogger(ShapefileExporter.class.getName()).log(Level.SEVERE, null, ex); }SVG Files: SVG Files

svg_test.svg is the custom one I am trying to convert the coordinates, currently does not display (will display, but very small with viewbox="-74 0 74 74")

svg_export.svg is auto generated using svgGenerator code.

Note: Could not post svg file contents, was too large. Posted link to dropbox folder with both of them in it.

Edit: I suppose my question isn't direct enough to be answered, so I will lay it out plainly.

What I am wanting to do is convert:

to this:

Or construct the SVG with the first set of coordinates to display properly (currently it does not as explained above.)

Update: I managed to figure out how to properly display the map in the svg file. However I don't really like how I'm doing it and could really use a different method. Currently I am adding:

I could really use whatever code is used to convert the map coordinates to screen/pixel coordinates.



أكثر...
 
أعلى