-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Geo Shape Query #1058
Open
heltonmarx
wants to merge
2
commits into
olivere:release-branch.v6
Choose a base branch
from
heltonmarx:geo_shape_query
base: release-branch.v6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package geo | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// Circle specified by a center point and radius with units, | ||
// which default to meters. | ||
type Circle struct { | ||
Type string `json:"type"` | ||
Radius string `json:"radius"` | ||
Coordinates []float64 `json:"coordinates"` | ||
} | ||
|
||
// NewCircle creates and initializes a new Circle. | ||
func NewCircle(radius string, coordinates []float64) *Circle { | ||
return &Circle{ | ||
Type: TypeCircle, | ||
Radius: radius, | ||
Coordinates: coordinates, | ||
} | ||
} | ||
|
||
// UnmarshalJSON decodes the circle data into a GeoJSON geometry. | ||
func (m *Circle) UnmarshalJSON(data []byte) error { | ||
raw := &rawGeometry{} | ||
if err := json.Unmarshal(data, raw); err != nil { | ||
return err | ||
} | ||
return m.decode(raw) | ||
} | ||
|
||
func (m *Circle) decode(raw *rawGeometry) error { | ||
m.Type = raw.Type | ||
m.Radius = raw.Radius | ||
return json.Unmarshal(raw.Coordinates, &m.Coordinates) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package geo | ||
|
||
import "encoding/json" | ||
|
||
// Envelope consists of coordinates for upper left and lower right points of the shape | ||
// to represent a bounding rectangle. | ||
type Envelope struct { | ||
Type string `json:"type"` | ||
Coordinates [][]float64 `json:"coordinates"` | ||
} | ||
|
||
// NewEnvelope creates and initializes a new Rectangle. | ||
func NewEnvelope(coordinates [][]float64) *Envelope { | ||
return &Envelope{ | ||
Type: TypeEnvelope, | ||
Coordinates: coordinates, | ||
} | ||
} | ||
|
||
// UnmarshalJSON decodes the envelope data into a GeoJSON geometry. | ||
func (m *Envelope) UnmarshalJSON(data []byte) error { | ||
raw := &rawGeometry{} | ||
if err := json.Unmarshal(data, raw); err != nil { | ||
return err | ||
} | ||
return m.decode(raw) | ||
} | ||
|
||
func (m *Envelope) decode(raw *rawGeometry) error { | ||
m.Type = raw.Type | ||
return json.Unmarshal(raw.Coordinates, &m.Coordinates) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package geo | ||
|
||
// Examples from RFC7946 | ||
// https://tools.ietf.org/html/rfc7946 | ||
|
||
var point = []byte(` | ||
{ | ||
"type":"point", | ||
"coordinates":[100.0, 0.0] | ||
} | ||
`) | ||
|
||
var multiPoint = []byte(` | ||
{ | ||
"type":"multipoint", | ||
"coordinates":[ | ||
[100.0, 0.0], | ||
[101.0, 1.0] | ||
] | ||
} | ||
`) | ||
|
||
var lineString = []byte(` | ||
{ | ||
"type":"linestring", | ||
"coordinates":[ | ||
[100.0, 0.0], | ||
[101.0, 1.0] | ||
] | ||
} | ||
`) | ||
|
||
var multiLineString = []byte(` | ||
|
||
{ | ||
"type":"multilinestring", | ||
"coordinates":[ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 1.0] | ||
], | ||
[ | ||
[102.0, 2.0], | ||
[103.0, 3.0] | ||
] | ||
] | ||
} | ||
`) | ||
|
||
var polygon = []byte(` | ||
{ | ||
"type":"polygon", | ||
"coordinates":[ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
] | ||
] | ||
} | ||
`) | ||
|
||
var polygonWithHoles = []byte(` | ||
{ | ||
"type":"polygon", | ||
"coordinates":[ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
], | ||
[ | ||
[100.2, 0.2], | ||
[100.8, 0.2], | ||
[100.8, 0.8], | ||
[100.2, 0.8], | ||
[100.2, 0.2] | ||
] | ||
] | ||
} | ||
|
||
`) | ||
|
||
var multiPolygon = []byte(` | ||
{ | ||
"type":"multipolygon", | ||
"coordinates":[ | ||
[ | ||
[ | ||
[102.0, 2.0], | ||
[103.0, 2.0], | ||
[103.0, 3.0], | ||
[102.0, 3.0], | ||
[102.0, 2.0] | ||
] | ||
], | ||
[ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
], | ||
[ | ||
[100.2, 0.2], | ||
[100.8, 0.2], | ||
[100.8, 0.8], | ||
[100.2, 0.8], | ||
[100.2, 0.2] | ||
] | ||
] | ||
] | ||
} | ||
`) | ||
|
||
var geometryCollection = []byte(` | ||
{ | ||
"type":"geometrycollection", | ||
"geometries":[ | ||
{ | ||
"type":"point", | ||
"coordinates":[100.0, 0.0] | ||
}, | ||
{ | ||
"type":"linestring", | ||
"coordinates":[ | ||
[101.0, 0.0], | ||
[102.0, 1.0] | ||
] | ||
} | ||
] | ||
} | ||
`) | ||
|
||
var envelope = []byte(` | ||
{ | ||
"type":"envelope", | ||
"coordinates":[ | ||
[100.0, 1.0], | ||
[101.0, 0.0] | ||
] | ||
} | ||
`) | ||
|
||
var circle = []byte(` | ||
{ | ||
"type":"circle", | ||
"radius":"25m", | ||
"coordinates":[-109.874838, 44.439550] | ||
} | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package geo | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// The geometry types supported by Elasticsearch. | ||
// | ||
// For more details, see: | ||
// https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#spatial-strategy | ||
const ( | ||
TypePoint = "point" | ||
TypeMultiPoint = "multipoint" | ||
TypeLineString = "linestring" | ||
TypeMultiLineString = "multilinestring" | ||
TypePolygon = "polygon" | ||
TypeMultiPolygon = "multipolygon" | ||
TypeGeometryCollection = "geometrycollection" | ||
TypeEnvelope = "envelope" | ||
TypeCircle = "circle" | ||
) | ||
|
||
// rawGeometry holds generic data used to unmarshal GeoJSON information. | ||
type rawGeometry struct { | ||
Type string `json:"type"` | ||
Coordinates json.RawMessage `json:"coordinates"` | ||
Geometries []rawGeometry `json:"geometries"` | ||
Radius string `json:"radius,omitempty"` | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a method to verify the arguments?
for example: the order of coordinates, Lon and Lat or Lat and Lon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, no, there's not, I will implement it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some functions like this in
geo_point.go
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are roughly based on
GeoPoint.java
.