osmgeojson

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 5 Imported by: 8

README

osm/osmgeojson Godoc Reference

Package osmgeojson converts OSM data to GeoJSON. It is a full port of the nodejs library osmtogeojson and sports the same features and tests (plus more):

  • real OSM polygon detection
  • OSM multipolygon support, e.g. buildings with holes become proper multipolygons
  • supports annotated geometries
  • well tested
Usage
delta := 0.0001

lon, lat := -83.5997038, 41.5923682
bounds := &osm.Bounds{
	MinLat: lat - delta, MaxLat: lat + delta,
	MinLon: lon - delta, MaxLon: lon + delta,
}

o, _ := osmapi.Map(ctx, bounds)  // fetch data from the osm api.

// run the conversion
fc, err := osmgeojson.Convert(o, opts)

// marshal the json
gj, _ := json.MarshalIndent(fc, "", " ")
fmt.Println(string(gj))
Options

The package provides several options to control what is included in the feature properties. If possible, excluding some of the extra properties can greatly improve the performance. All of the options default to false, i.e. everything will be included.

  • NoID(yes bool)

    Controls whether to set the feature.ID to "type/id" e.g. "node/475373687". For some use cases this may be of limited use since the feature.Properies "type" and "id" are also set.

  • NoMeta(yes bool)

    Controls whether to populate the "meta" property which is a sub-map with the following values from the osm element: "timestamp", "version", "changeset", "user", "uid".

  • NoRelationMembership(yes bool)

    Controls whether to include a list of the relations the osm element is a member of. This info is set as the "relation" property which is an array of objects with the following values from the relation: "id", "role", "tags".

  • IncludeInvalidPolygons(yes bool)

    By default, inner rings of 'multipolygon' without a matching outer ring will be ignored. However, in some use cases the outer ring can be implied as the viewport bound and the inner rings can then be rendered correctly. Polygons with a nil first ring will be need to be updated such that the first ring is the viewport bound. This options will also include rings that do not have matching endpoints. Usually this means one or more of the outer ways are missing.

Benchmarks

These benchmarks are meant to show the performance impact of the different options. They were run on a 2012 MacBook Air with a 2 ghz processor and 8 gigs of ram.

BenchmarkConvert-4                        10000     2520891 ns/op     935697 B/op     11299 allocs/op
BenchmarkConvertAnnotated-4               10000     2196433 ns/op     853544 B/op     11239 allocs/op
BenchmarkConvert_NoID-4                   10000     2310816 ns/op     913915 B/op      9687 allocs/op
BenchmarkConvert_NoMeta-4                 10000     2026031 ns/op     716953 B/op      7546 allocs/op
BenchmarkConvert_NoRelationMembership-4   10000     2397634 ns/op     912454 B/op     10716 allocs/op
BenchmarkConvert_NoIDsMetaMembership-4    20000     1718224 ns/op     671984 B/op      5353 allocs/op
Similar libraries in other languages:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(o *osm.OSM, opts ...Option) (*geojson.FeatureCollection, error)

Convert takes a set of osm elements and converts them to a geojson feature collection.

Types

type Option

type Option func(*context) error

An Option is a setting for creating the geojson.

func IncludeInvalidPolygons

func IncludeInvalidPolygons(yes bool) Option

IncludeInvalidPolygons will return a polygon with nil outer/first ring if the outer ringer is not found in the data. It may also return rings whose endpoints do not match and are probably missing sections.

func NoID

func NoID(yes bool) Option

NoID will omit setting the geojson feature.ID

func NoMeta

func NoMeta(yes bool) Option

NoMeta will omit the meta (timestamp, user, changeset, etc) info from the output geojson feature properties.

func NoRelationMembership

func NoRelationMembership(yes bool) Option

NoRelationMembership will omit the the list of relations an element is a member of from the output geojson features.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL