Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d6e6e2e
Add initial encoder implementation
larsbeck Sep 1, 2023
c2da18d
Fix module references
larsbeck Sep 1, 2023
3b71c2a
Use getters for default values
larsbeck Sep 1, 2023
a6a7508
Write correct osmType to blob
larsbeck Sep 1, 2023
86cc279
Writing some info about header sizes
larsbeck Sep 1, 2023
53ca237
More output
larsbeck Sep 1, 2023
99a5385
Write size as binary.BigEndian
larsbeck Sep 1, 2023
833d1d4
Add encoder tests
larsbeck Sep 2, 2023
7589006
Protect write by lock
larsbeck Sep 2, 2023
b51b739
Protect WriteObject by lock
larsbeck Sep 2, 2023
e559d41
Flush if entity type changed
larsbeck Sep 2, 2023
fc1139b
Deactivate tags and denseinfo
larsbeck Sep 2, 2023
80feaae
Readd tags
larsbeck Sep 2, 2023
768d6e3
Fix tags
larsbeck Sep 2, 2023
f47a975
Write 0 in any case
larsbeck Sep 2, 2023
42429ab
Add denseinfo
larsbeck Sep 2, 2023
fad147b
Rearrange fields
larsbeck Sep 2, 2023
44f1f4e
Rename writer to encoder
larsbeck Sep 6, 2023
e980ace
Merge pull request #1 from nextmv-io/add-osm-pbf-encoder
larsbeck Oct 13, 2023
9eeab80
Add OSM encoder
larsbeck Nov 13, 2023
48c0be5
Revert auto-formatting related changes
larsbeck Nov 13, 2023
73659be
Remove auto-formatting newline comment
larsbeck Nov 13, 2023
656c127
update github actions, add linter action
paulmach Dec 27, 2023
89f4b82
fix golangci-lint errors
paulmach Dec 28, 2023
b3aa927
correctly unmarshal elements with a type tag
paulmach Dec 27, 2023
f9fed71
fix lint errors: remove ioutil references and other fixes
paulmach Dec 28, 2023
63649b3
update changelog
paulmach Jan 8, 2024
fbd6841
osmpbf examples: fix small typos
bdon Jan 10, 2024
db9d8eb
fix whitespace
bdon Jan 10, 2024
4acd5c6
Merge branch 'master' into larsbeck/add-osm-pbf-encoder
larsbeck Apr 16, 2024
af1fec1
Fix delta encoding of a node's timestamp on DenseInfo field
larsbeck May 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.21'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55
25 changes: 9 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,28 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: '1.15.3'

- name: Install dependencies
run: |
go version
go get -u golang.org/x/lint/golint
go-version: '1.21'

- name: Run build
run: go build .
- name: Run vet & lint
run: go build .

- name: Run vet
run: |
go vet .
golint .


- name: Run tests
run: go test -v -coverprofile=profile.cov ./...

- name: codecov
uses: codecov/codecov-action@v1
with:
Expand Down
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- errcheck
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to this project will be documented in this file.

## [v0.8.0](https://github.com/paulmach/osm/compare/v0.7.1...v0.8.0) - 2024-01-08

### Changed

- go 1.16 is required, updated usages of `ioutil` for similar functions in `io` and `os`

### Fixed

- correctly JSON unmarshal elements with a type tag by [@paulmach](https://github.com/paulmach) in https://github.com/paulmach/osm/pull/53

## [v0.7.1](https://github.com/paulmach/osm/compare/v0.7.0...v0.7.1) - 2022-11-29

### Added
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/paulmach/osm"
)

var c = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: false,
MarshalFloatWith6Digits: true,
}.Froze()
CustomJSONMarshaler = c
CustomJSONUnmarshaler = c

osm.CustomJSONMarshaler = c
osm.CustomJSONUnmarshaler = c
```

The above change can have dramatic performance implications, see the benchmarks below
Expand Down
2 changes: 1 addition & 1 deletion annotate/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func wayCentroid(w *osm.Way) orb.Point {
return point
}

// orientation will annotate the the orientation of multipolygon relation members.
// orientation will annotate the orientation of multipolygon relation members.
// This makes it possible to reconstruct relations with partial data in the right direction.
// Return value indicates if the result is 'tainted', e.g. not all way members were present.
func orientation(members osm.Members, ways map[osm.WayID]*osm.Way, at time.Time) bool {
Expand Down
7 changes: 5 additions & 2 deletions annotate/relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -42,7 +42,10 @@ func TestRelation(t *testing.T) {
t.Errorf("expected relations not equal, file saved to %s", filename)

data, _ := xml.MarshalIndent(&osm.OSM{Relations: relations}, "", " ")
ioutil.WriteFile(filename, data, 0644)
err := os.WriteFile(filename, data, 0644)
if err != nil {
t.Fatalf("write error: %v", err)
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions annotate/way_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -36,7 +36,10 @@ func TestWays(t *testing.T) {
t.Errorf("expected way not equal, file saved to %s", filename)

data, _ := xml.MarshalIndent(&osm.OSM{Ways: o.Ways}, "", " ")
ioutil.WriteFile(filename, data, 0644)
err := os.WriteFile(filename, data, 0644)
if err != nil {
t.Fatalf("write error: %v", err)
}
}
}
}
Expand Down Expand Up @@ -128,7 +131,7 @@ func BenchmarkWays(b *testing.B) {
}

func loadTestdata(tb testing.TB, filename string) *osm.OSM {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
tb.Fatalf("unable to open file: %v", err)
}
Expand Down
37 changes: 12 additions & 25 deletions change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"encoding/xml"
"io/ioutil"
"os"
"testing"
)

Expand Down Expand Up @@ -236,29 +236,14 @@ func TestChange_HistoryDatasource(t *testing.T) {
}
}

func cleanXMLNameFromChange(c *Change) {
c.Version = ""
c.Generator = ""
c.Copyright = ""
c.Attribution = ""
c.License = ""
if c.Create != nil {
cleanXMLNameFromOSM(c.Create)
}
if c.Modify != nil {
cleanXMLNameFromOSM(c.Modify)
}
if c.Delete != nil {
cleanXMLNameFromOSM(c.Delete)
}
}

func BenchmarkChange_MarshalXML(b *testing.B) {
filename := "testdata/changeset_38162206.osc"
data := readFile(b, filename)
data, err := os.ReadFile("testdata/changeset_38162206.osc")
if err != nil {
b.Fatalf("unable to read file: %v", err)
}

c := &Change{}
err := xml.Unmarshal(data, c)
err = xml.Unmarshal(data, c)
if err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
Expand Down Expand Up @@ -287,7 +272,7 @@ func BenchmarkChange_MarshalXML(b *testing.B) {
// }

func BenchmarkChange_MarshalJSON(b *testing.B) {
data, err := ioutil.ReadFile("testdata/minute_871.osc")
data, err := os.ReadFile("testdata/minute_871.osc")
if err != nil {
b.Fatalf("could not read file: %v", err)
}
Expand All @@ -309,7 +294,7 @@ func BenchmarkChange_MarshalJSON(b *testing.B) {
}

func BenchmarkChange_UnmarshalJSON(b *testing.B) {
data, err := ioutil.ReadFile("testdata/minute_871.osc")
data, err := os.ReadFile("testdata/minute_871.osc")
if err != nil {
b.Fatalf("could not read file: %v", err)
}
Expand Down Expand Up @@ -337,8 +322,10 @@ func BenchmarkChange_UnmarshalJSON(b *testing.B) {
}

func BenchmarkChangeset_UnmarshalXML(b *testing.B) {
filename := "testdata/changeset_38162206.osc"
data := readFile(b, filename)
data, err := os.ReadFile("testdata/changeset_38162206.osc")
if err != nil {
b.Fatalf("unable to read file: %v", err)
}

b.ReportAllocs()
b.ResetTimer()
Expand Down
30 changes: 23 additions & 7 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package osm

import (
"encoding/xml"
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -72,10 +73,13 @@ func TestDiff_MarshalXML(t *testing.T) {
}

func TestDiff(t *testing.T) {
data := readFile(t, "testdata/annotated_diff.xml")
data, err := os.ReadFile("testdata/annotated_diff.xml")
if err != nil {
t.Fatalf("unable to read file: %v", err)
}

diff := &Diff{}
err := xml.Unmarshal(data, &diff)
err = xml.Unmarshal(data, &diff)
if err != nil {
t.Errorf("unable to unmarshal: %v", err)
}
Expand Down Expand Up @@ -137,28 +141,40 @@ func TestDiff(t *testing.T) {
}

func BenchmarkDiff_Marshal(b *testing.B) {
data := readFile(b, "testdata/annotated_diff.xml")
data, err := os.ReadFile("testdata/annotated_diff.xml")
if err != nil {
b.Fatalf("unable to read file: %v", err)
}

diff := &Diff{}
err := xml.Unmarshal(data, &diff)
err = xml.Unmarshal(data, &diff)
if err != nil {
b.Fatalf("unmarshal error: %v", err)
}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
xml.Marshal(diff)
_, err := xml.Marshal(diff)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}

func BenchmarkDiff_Unmarshal(b *testing.B) {
data := readFile(b, "testdata/annotated_diff.xml")
data, err := os.ReadFile("testdata/annotated_diff.xml")
if err != nil {
b.Fatalf("unable to read file: %v", err)
}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
diff := &Diff{}
xml.Unmarshal(data, &diff)
err := xml.Unmarshal(data, &diff)
if err != nil {
b.Fatalf("unmarshal error: %v", err)
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/paulmach/osm

go 1.13
go 1.16

require (
github.com/datadog/czlib v0.0.0-20160811164712-4bc9a24e37f2
Expand Down
19 changes: 13 additions & 6 deletions osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package osm
import (
"encoding/xml"
"fmt"
"regexp"
)

// These values should be returned if the osm data is actual
Expand Down Expand Up @@ -367,13 +366,21 @@ func (o *OSM) UnmarshalJSON(data []byte) error {
return nil
}

var jsonTypeRegexp = regexp.MustCompile(`"type"\s*:\s*"([^"]*)"`)
type typeStruct struct {
Type string `json:"type"`
}

func findType(index int, data []byte) (string, error) {
matches := jsonTypeRegexp.FindAllSubmatch(data, 1)
if len(matches) > 0 {
return string(matches[0][1]), nil
ts := typeStruct{}
err := unmarshalJSON(data, &ts)
if err != nil {
// should not happened due to previous decoding succeeded
return "", err
}

if ts.Type == "" {
return "", fmt.Errorf("could not find type in element index %d", index)
}

return "", fmt.Errorf("could not find type in element index %d", index)
return ts.Type, nil
}
Loading