1

I have been using the library draw2d with go-nexrad, and I am able to generate PNG and SVG files. However, the SVG files that are generated are enormous, with a less than 10MB PNG file becoming a 28MB SVG file with the same options. Here is the code:

func render(out string, radials []*archive2.Message31, label string) {

    width := float64(imageSize)
    height := float64(imageSize)

    PNGcanvas := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
    draw.Draw(PNGcanvas, PNGcanvas.Bounds(), image.Black, image.ZP, draw.Src)
    PNGgc := draw2dimg.NewGraphicContext(PNGcanvas)

    SVGcanvas := draw2dsvg.NewSvg()
    SVGcanvas.Width = strconv.Itoa(int(width)) + "px"
    SVGcanvas.Height = strconv.Itoa(int(width)) + "px"

    SVGgc := draw2dsvg.NewGraphicContext(SVGcanvas)

    xc := width / 2
    yc := height / 2
    pxPerKm := width / 2 / 460
    firstGatePx := float64(radials[0].ReflectivityData.DataMomentRange) / 1000 * pxPerKm
    gateIntervalKm := float64(radials[0].ReflectivityData.DataMomentRangeSampleInterval) / 1000
    gateWidthPx := gateIntervalKm * pxPerKm

    t := time.Now()
    log.Println("rendering radials")
    // valueDist := map[float32]int{}

    for _, radial := range radials {
        // round to the nearest rounded azimuth for the given resolution.
        // ex: for radial 20.5432, round to 20.5
        azimuthAngle := float64(radial.Header.AzimuthAngle) - 90
        if azimuthAngle < 0 {
            azimuthAngle = 360.0 + azimuthAngle
        }
        azimuthSpacing := radial.Header.AzimuthResolutionSpacing()
        azimuth := math.Floor(azimuthAngle)
        if math.Floor(azimuthAngle+azimuthSpacing) > azimuth {
            azimuth += azimuthSpacing
        }
        startAngle := azimuth * (math.Pi / 180.0)      /* angles are specified */
        endAngle := azimuthSpacing * (math.Pi / 180.0) /* clockwise in radians           */

        // start drawing gates from the start of the first gate
        distanceX, distanceY := firstGatePx, firstGatePx
        if vectorize == "png" {
            PNGgc.SetLineWidth(gateWidthPx + 1)
        } else if vectorize == "svg" {
            SVGgc.SetLineWidth(gateWidthPx + 1)
        }
        if vectorize == "png" {
            PNGgc.SetLineCap(draw2d.ButtCap)
        } else if vectorize == "svg" {
            SVGgc.SetLineCap(draw2d.ButtCap)
        }

        var gates []float32
        switch product {
        case "vel":
            gates = radial.VelocityData.ScaledData()
        case "sw":
            gates = radial.SwData.ScaledData()
        case "rho":
            gates = radial.RhoData.ScaledData()
        default:
            gates = radial.ReflectivityData.ScaledData()
        }

        numGates := len(gates)
        for i, v := range gates {
            if v != archive2.MomentDataBelowThreshold {

                //fmt.Println(gateWidthPx)
                if i == 0 {
                    SVGgc.SetLineWidth(0)
                } else if i > 0 {
                    SVGgc.SetLineWidth(gateWidthPx + 1)
                }

                // valueDist[v] += 1

                if vectorize == "png" {
                    PNGgc.MoveTo(xc+math.Cos(startAngle)*distanceX, yc+math.Sin(startAngle)*distanceY)
                } else if vectorize == "svg" {
                    SVGgc.MoveTo(xc+math.Cos(startAngle)*distanceX, yc+math.Sin(startAngle)*distanceY)
                }

                // make the gates connect visually by extending arcs so there is no space between adjacent gates.
                if i == 0 {
                    if vectorize == "png" {
                        PNGgc.ArcTo(xc, yc, distanceX, distanceY, startAngle-.001, endAngle+.001)
                    } else if vectorize == "svg" {
                        SVGgc.ArcTo(xc, yc, distanceX, distanceY, startAngle-.001, endAngle+.001)
                    }
                } else if i == numGates-1 {
                    if vectorize == "png" {
                        PNGgc.ArcTo(xc, yc, distanceX, distanceY, startAngle, endAngle)
                    } else if vectorize == "svg" {
                        SVGgc.ArcTo(xc, yc, distanceX, distanceY, startAngle, endAngle)
                    }
                } else {
                    if vectorize == "png" {
                        PNGgc.ArcTo(xc, yc, distanceX, distanceY, startAngle, endAngle+.001)
                    } else if vectorize == "svg" {
                        SVGgc.ArcTo(xc, yc, distanceX, distanceY, startAngle, endAngle+.001)
                    }
                }

                if vectorize == "png" {
                    PNGgc.SetStrokeColor(colorSchemes[product][colorScheme](v))
                } else if vectorize == "svg" {
                    SVGgc.SetStrokeColor(colorSchemes[product][colorScheme](v))
                }
                if vectorize == "png" {
                    PNGgc.Stroke()
                } else if vectorize == "svg" {
                    SVGgc.Stroke()
                }
            }

            distanceX += gateWidthPx
            distanceY += gateWidthPx
            azimuth += radial.Header.AzimuthResolutionSpacing()
        }
    }

    // fmt.Println(valueDist)

    if renderLabel {
        if vectorize == "png" {
            addLabel(PNGcanvas, int(width-495.0), int(height-10.0), label)
        } else if vectorize == "svg" {
            logrus.Warn("Labels cannot be drawn on an SVG image, ignoring -L flag")
        }
    }

    // Save to file
    if vectorize == "png" {
        draw2dimg.SaveToPngFile(out, PNGcanvas)
        fmt.Println("Finished in", time.Since(t))
    } else if vectorize == "svg" {
        draw2dsvg.SaveToSvgFile(out, SVGcanvas)
        fmt.Println("Finished in", time.Since(t))
    }
}

The full file can be found in my fork of the project here.

The reason I think the SVG is so large is because it is generating the file very inefficiently, possibly by trying to render every pixel instead of just a start and end point. I have tried setting the DPI with SVGgc.setDPI(), but that hasn't worked.

If anyone has any idea about why the file is so large, or any idea of how to fix it, I would greatly appreciate your input. Hopefully you won't have to go through the entire go-nexrad project to understand this, I have included the code block that I am almost certain is causing the issue, and is the part that uses the library.

Pufferfishe
  • 139
  • 1
  • 7
  • 1
    But what is the question? If it's "why they are so big?", it's a question for the issue tracker or discussion board of the relevant projects, I suppose, not SO. – kostix Jun 09 '22 at 16:17
  • I don't think it's an issue with the library, I think it's an issue with the code I have written in how it's being rendered. This is why I came to SO, not the issue tracker. @kostix – Pufferfishe Jun 09 '22 at 16:18
  • Have you tried to reduce the problem? Have you tested individual parts of the code to see that they do what you think they should do? Have you tried reading the SVG file to figure out what's taking up the bulk? (It should be XML / human readable). – Hymns For Disco Jun 10 '22 at 03:57

0 Answers0