The add_geojson method allows you to load and render GeoJSON files directly onto a map.
Each GeoJSON file is parsed, classified, styled, and rendered as a map layer.
Note libgd-gis is evolving very fast, so some examples may temporarily stop working. Please report issues or ask for help: https://github.com/ggerman/libgd-gis/issues or ggerman@gmail.com
require "gd/gis"
require "gd"
TOKYO = [139.68, 35.63, 139.82, 35.75]
map = GD::GIS::Map.new(
bbox: TOKYO,
zoom: 13,
basemap: :esri_satellite
)
map.style = GD::GIS::Style.load("solarized")
map.add_geojson("railways.geojson")
map.add_geojson("parks.geojson")
map.add_geojson("wards.geojson")
map.render
map.save("tokyo.png")
add_geojson doesWhen calling add_geojson, libgd-gis:
Each call adds a new layer on top of the previous ones.
PointMultiPointLineStringMultiLineStringPolygonMultiPolygonRendering behavior depends on the active style.
GeoJSON layers are rendered in the order they are added:
map.add_geojson("base.geojson")
map.add_geojson("overlay.geojson")
Later layers are drawn on top of earlier ones.
add_geojson relies entirely on the active GD::GIS::Style.
See examples/styles/ for reference styles.
After rendering, you can open the generated image and add custom overlays
using ruby-libgd directly.
Example: adding a label overlay
img = GD::Image.open("tokyo.png")
font = "../fonts/DejaVuSans-Bold.ttf"
img.filled_rectangle(24, 24, 264, 88, [0, 0, 0])
img.text(
"TOKYO",
x: 48,
y: 68,
size: 32,
color: [255, 255, 255],
font: font
)
img.save("tokyo.png")