Skip to content

Commit

Permalink
Merge pull request #84 from xiaogu-space/main
Browse files Browse the repository at this point in the history
update geojson IsValid judgment method
  • Loading branch information
xiaogu-space authored Jul 27, 2022
2 parents 241f339 + 10c0004 commit 96858dc
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 12 deletions.
19 changes: 18 additions & 1 deletion example/data/geojson.json
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102,0.5]},"properties":{"prop0":"value0"}}]}
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
102,
0.5
]
},
"properties": {
"prop0": "value0"
}
}
]
}
93 changes: 89 additions & 4 deletions geoencoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestReadGeoJSON(t *testing.T) {
"type": "MultiPolygon",
"coordinates": [
[
[
113.25094290192146,
22.420572852340314
Expand All @@ -248,7 +248,7 @@ func TestReadGeoJSON(t *testing.T) {
113.25094290192146,
22.420572852340314
]
]
]
}
Expand All @@ -258,6 +258,91 @@ func TestReadGeoJSON(t *testing.T) {
want: nil,
wantErr: true,
},
{name: "geojson string", args: args{
[]byte(`{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-47.900390625,
-14.944784875088372
],
[
-51.591796875,
-19.91138351415555
],
[
-41.11083984375,
-21.309846141087192
],
[
-43.39599609375,
-15.390135715305204
]
]
]
]
},
"properties": {
"prop0": "value0"
}
}
]
}`), GeoJSON},
want: nil,
wantErr: false,
},
{name: "geojson string", args: args{
[]byte(`{
"type": "FeatureCollection",
"features": [
{
"_id": "pwBn4x",
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-48.368811019,
-23.906519878
],
[
-51.58525,
-19.90553
],
[
-41.1044,
-21.30375
],
[
-43.38951,
-15.38404
],
[
-48.368811019,
-23.906519878
]
]
]
]
},
"properties": {
"prop0": "value0"
}
}
]
}`), GeoJSON},
want: nil,
wantErr: false,
},
{name: "geojson string", args: args{
[]byte(`{"type": "FeatureCollection",
"features": [
Expand All @@ -267,7 +352,7 @@ func TestReadGeoJSON(t *testing.T) {
"type": "Polygon",
"coordinates": [
[
[
113.25094290192146,
22.420572852340314
Expand All @@ -288,7 +373,7 @@ func TestReadGeoJSON(t *testing.T) {
113.20524690238221,
22.46715598456933
]
]
]
}
Expand Down
29 changes: 23 additions & 6 deletions geoencoding/geojson/feature_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,32 @@ func UnmarshalFeatureCollection(data []byte) (*FeatureCollection, error) {
if !space.Ring(ring).IsClosed() {
poly[i] = append(ring, ring[0])
}

}
} else if mult, ok := v.Geometry.Geometry().(space.MultiPolygon); ok {
for _, poly := range mult {
for i, ring := range poly {
if !space.Ring(ring).IsClosed() {
poly[i] = append(ring, ring[0])
}
}
}
}
if !v.Geometry.Geometry().IsValid() {
if v.Geometry.Geometry().GeoJSONType() == space.TypePolygon {
v.Geometry.Coordinates = space.TransGeometry(
operation.CorrectPolygonMatrixSelfIntersect(
v.Geometry.Geometry().(space.Polygon).ToMatrix()))

if v.Geometry.Geometry().IsCorrect() {
if !v.Geometry.Geometry().IsValid() {
if v.Geometry.Geometry().GeoJSONType() == space.TypePolygon {
v.Geometry.Coordinates = space.TransGeometry(
operation.CorrectPolygonMatrixSelfIntersect(
v.Geometry.Geometry().(space.Polygon).ToMatrix()))
} else if v.Geometry.Geometry().GeoJSONType() == space.TypeMultiPolygon {
mult := v.Geometry.Geometry().(space.MultiPolygon)
for _, poly := range mult {
v.Geometry.Coordinates = space.TransGeometry(
operation.CorrectPolygonMatrixSelfIntersect(poly.ToMatrix()))
}
}
}
} else {
return nil, ErrInvalidGeometry
}
}
Expand Down
5 changes: 5 additions & 0 deletions space/bound.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ func (b Bound) IsValid() bool {
return b.Min.IsValid() && b.Max.IsValid()
}

// IsCorrect returns true if the geometry struct is Correct.
func (b Bound) IsCorrect() bool {
return b.Min.IsCorrect() && b.Max.IsCorrect()
}

// CoordinateSystem return Coordinate System.
func (b Bound) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
10 changes: 10 additions & 0 deletions space/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ func (c Collection) IsValid() bool {
return true
}

// IsCorrect returns true if the geometry struct is Correct.
func (c Collection) IsCorrect() bool {
for _, v := range c {
if !v.IsCorrect() {
return false
}
}
return true
}

// CoordinateSystem return Coordinate System.
func (c Collection) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
3 changes: 3 additions & 0 deletions space/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type Geometry interface {
// IsValid returns true if the geometry is valid.
IsValid() bool

// IsCorrect returns true if the geometry struct is Correct.
IsCorrect() bool

// Length Returns the length of this geometry
Length() float64

Expand Down
13 changes: 13 additions & 0 deletions space/line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,19 @@ func (ls LineString) IsValid() bool {
return true
}

// IsCorrect returns true if the geometry struct is Correct.
func (ls LineString) IsCorrect() bool {
if ls.IsEmpty() {
return false
}
for _, v := range ls {
if !Point(v).IsCorrect() {
return false
}
}
return true
}

// CoordinateSystem return Coordinate System.
func (ls LineString) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
10 changes: 10 additions & 0 deletions space/multi_line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ func (mls MultiLineString) IsValid() bool {
return true
}

// IsCorrect returns true if the geometry struct is Correct.
func (mls MultiLineString) IsCorrect() bool {
for _, v := range mls {
if !v.IsCorrect() {
return false
}
}
return true
}

// CoordinateSystem return Coordinate System.
func (mls MultiLineString) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
10 changes: 10 additions & 0 deletions space/multi_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ func (mp MultiPoint) IsValid() bool {
return true
}

// IsCorrect returns true if the geometry struct is Correct.
func (mp MultiPoint) IsCorrect() bool {
for _, v := range mp {
if !v.IsCorrect() {
return false
}
}
return true
}

// CoordinateSystem return Coordinate System.
func (mp MultiPoint) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
10 changes: 10 additions & 0 deletions space/multi_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ func (mp MultiPolygon) IsValid() bool {
return true
}

// IsCorrect returns true if the geometry struct is Correct.
func (mp MultiPolygon) IsCorrect() bool {
for _, v := range mp {
if !v.IsCorrect() {
return false
}
}
return true
}

// CoordinateSystem return Coordinate System.
func (mp MultiPolygon) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
5 changes: 5 additions & 0 deletions space/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ func (p Point) IsRing() bool {

// IsValid returns true if the geometry is valid.
func (p Point) IsValid() bool {
return p.IsCorrect()
}

// IsCorrect returns true if the geometry struct is Correct.
func (p Point) IsCorrect() bool {
return len(p) >= 2
}

Expand Down
13 changes: 13 additions & 0 deletions space/polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ func (p Polygon) IsValid() bool {
return true
}

// IsCorrect returns true if the geometry struct is Correct.
func (p Polygon) IsCorrect() bool {
if p.IsEmpty() {
return false
}
for _, v := range p {
if !Ring(v).IsCorrect() {
return false
}
}
return true
}

// CoordinateSystem return Coordinate System.
func (p Polygon) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down
7 changes: 6 additions & 1 deletion space/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,16 @@ func (r Ring) IsRing() bool {
return LineString(r).IsRing()
}

// IsValid returns true if the geometry is valid.
// IsValid returns true if the geometry is valid.
func (r Ring) IsValid() bool {
return LineString(r).IsValid() && (!r.IsEmpty()) && r.IsRing()
}

// IsCorrect returns true if the geometry struct is Correct.
func (r Ring) IsCorrect() bool {
return LineString(r).IsCorrect() && (!r.IsEmpty())
}

// CoordinateSystem return Coordinate System.
func (r Ring) CoordinateSystem() int {
return defaultCoordinateSystem()
Expand Down

0 comments on commit 96858dc

Please sign in to comment.