Skip to content

Commit

Permalink
Merge pull request #231 from felix-schott/202_ndgeojson
Browse files Browse the repository at this point in the history
Add support for ndgeojson
  • Loading branch information
tschaub authored Nov 18, 2024
2 parents 04fe2e0 + 51afc40 commit c2d82f5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
31 changes: 26 additions & 5 deletions cmd/gpq/command/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package command
import (
"net/url"
"os"
"path/filepath"
"slices"
"strings"

"github.com/planetlabs/gpq/internal/geojson"
Expand Down Expand Up @@ -63,19 +65,38 @@ func parseFormatType(format string) FormatType {
return ft
}

var geoParquetSuffixes = []string{
".gpq", ".geoparquet",
}

var parquetSuffixes = []string{
".pq", ".parquet",
}

var geoJsonSuffixes = []string{
".geojson",
".json",
".ndjson",
".ndgeojson",
".geojsonl",
}

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") {
return GeoJSONType
}
if strings.HasSuffix(resource, ".gpq") || strings.HasSuffix(resource, ".geoparquet") {

ext := filepath.Ext(resource)
if slices.Contains(geoParquetSuffixes, ext) {
return GeoParquetType
}
if strings.HasSuffix(resource, ".pq") || strings.HasSuffix(resource, ".parquet") {
if slices.Contains(parquetSuffixes, ext) {
return ParquetType
}
if slices.Contains(geoJsonSuffixes, ext) {
return GeoJSONType
}

return UnknownType
}

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
42 changes: 42 additions & 0 deletions internal/geojson/featurereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,48 @@ 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 TestFeatureReaderBadNewLineDelimited(t *testing.T) {
file, openErr := os.Open("testdata/bad-new-line-delimited.ndgeojson")
require.NoError(t, openErr)

reader := geojson.NewFeatureReader(file)

first, err := reader.Read()
require.NoError(t, err)
assert.Equal(t, "Oceania", first.Properties["continent"])

_, err = reader.Read()
assert.ErrorContains(t, err, "unexpected end of JSON input")
}

func TestFeatureReaderEmptyFeatureCollection(t *testing.T) {
file, openErr := os.Open("testdata/empty-collection.geojson")
require.NoError(t, openErr)
Expand Down
Loading

0 comments on commit c2d82f5

Please sign in to comment.