Skip to content

Commit

Permalink
add support for ndgeojson
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-schott committed Nov 18, 2024
1 parent 04fe2e0 commit 0bd83b1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/gpq/command/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package command
import (
"net/url"
"os"
"regexp"
"strings"

"github.com/planetlabs/gpq/internal/geojson"
Expand Down Expand Up @@ -52,6 +53,8 @@ var validTypes = map[FormatType]bool{
GeoJSONType: true,
}

var geoJsonMatcher *regexp.Regexp

func parseFormatType(format string) FormatType {
if format == "" {
return AutoType
Expand All @@ -67,7 +70,7 @@ func getFormatType(resource string) FormatType {
if u, err := url.Parse(resource); err == nil {
resource = u.Path
}
if strings.HasSuffix(resource, ".json") || strings.HasSuffix(resource, ".geojson") {
if geoJsonMatcher.MatchString(resource) {
return GeoJSONType
}
if strings.HasSuffix(resource, ".gpq") || strings.HasSuffix(resource, ".geoparquet") {
Expand Down Expand Up @@ -96,6 +99,8 @@ func (c *ConvertCmd) Run() error {
inputSource = ""
}

geoJsonMatcher = regexp.MustCompile(`.+\.(geojson|json|ndjson|ndgeojson|geojsonl)$`)

outputFormat := parseFormatType(c.To)
if outputFormat == AutoType {
if outputSource == "" {
Expand Down
3 changes: 3 additions & 0 deletions internal/geojson/featurereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (r *FeatureReader) Read() (*geo.Feature, error) {

delim, ok := keyToken.(json.Delim)
if ok && delim == json.Delim('}') {
if r.decoder.More() {
r.collection = true
}
if feature == nil {
return nil, errors.New("expected a FeatureCollection, a Feature, or a Geometry object")
}
Expand Down
28 changes: 28 additions & 0 deletions internal/geojson/featurereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ func TestFeatureReaderSingleFeature(t *testing.T) {
assert.Equal(t, map[string]any{"name": "test"}, feature.Properties)
}

func TestFeatureReaderNewLineDelimited(t *testing.T) {
file, openErr := os.Open("testdata/new-line-delimited.ndgeojson")
require.NoError(t, openErr)

reader := geojson.NewFeatureReader(file)

features := []*geo.Feature{}
for {
feature, err := reader.Read()
if err == io.EOF {
break
}
require.NoError(t, err)
features = append(features, feature)
}
require.Len(t, features, 5)

fiji := features[0]
assert.NotNil(t, fiji.Geometry)
assert.Equal(t, "Oceania", fiji.Properties["continent"])
assert.Equal(t, float64(920938), fiji.Properties["pop_est"])

usa := features[4]
assert.NotNil(t, usa.Geometry)
assert.Equal(t, "North America", usa.Properties["continent"])
assert.Equal(t, float64(326625791), usa.Properties["pop_est"])
}

func TestFeatureReaderEmptyFeatureCollection(t *testing.T) {
file, openErr := os.Open("testdata/empty-collection.geojson")
require.NoError(t, openErr)
Expand Down
5 changes: 5 additions & 0 deletions internal/geojson/testdata/new-line-delimited.ndgeojson

Large diffs are not rendered by default.

0 comments on commit 0bd83b1

Please sign in to comment.