Skip to content

Commit

Permalink
#33: Adds near(lat,long,tolerance) to the query language (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch authored Sep 10, 2024
1 parent e58ddd1 commit 8a4bdf7
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 69 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "e36efc60ff6513f9fd73eec6246018b16a077ae5221e7e54d785cf0a8c172a1a",
"originHash" : "cf3317819e9dd07905aa1240016b12528f485e5811ed0edacee39014b35850b9",
"pins" : [
{
"identity" : "gis-tools",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Outdooractive/gis-tools",
"state" : {
"revision" : "a8118bcd5e715a69f640476446fbf058fe39973f",
"version" : "1.8.3"
"revision" : "96aeecd98b0b2871e5f654d8888c7ea60fea8575",
"version" : "1.8.4"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let package = Package(
targets: ["MVTTools"]),
],
dependencies: [
.package(url: "https://github.com/Outdooractive/gis-tools", from: "1.8.3"),
.package(url: "https://github.com/Outdooractive/gis-tools", from: "1.8.4"),
.package(url: "https://github.com/1024jp/GzipSwift.git", from: "5.2.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.1"),
Expand Down
59 changes: 46 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ mvt query Tests/MVTToolsTests/TestData/14_8716_8015.geojson "3.87324,11.53731,10
}
```
---
**Example 3**: Query a tile for properties.
**Example 3**: Query Feature properties in a tile.
```bash
mvt query -p Tests/MVTToolsTests/TestData/14_8716_8015.vector.mvt ".area > 40000 and .class == 'hospital'"
Expand Down Expand Up @@ -346,9 +346,10 @@ mvt query -p Tests/MVTToolsTests/TestData/14_8716_8015.vector.mvt ".area > 40000
}
```
The query language is loosely modeled after the jq query language. Here is an overview.
The query language is very loosely modeled after the jq query language.
The output will contain all features where the query returns `true`.
Example:
Here is an overview. Example:
```
"properties": {
"foo": {"bar": 1},
Expand All @@ -359,13 +360,14 @@ Example:
```
Values are retrieved by putting a `.` in front of the property name. The property name must be quoted
if it is a number or contains any non-alphabetic characters. Elements in arrays can be
accesses either by simply using the array index after the dot, or by wrapping it in brackets.
if it is a number or contains non-alphabetic characters. Elements in arrays can be
accessed either by simply using the array index after the dot, or by wrapping it in brackets.
```
.foo // true, property "foo" exists
.foo.bar // true, property "foo" is a dictionary containing "bar"
."foo"."bar" // true, same as above but quoted
.'foo'.'bar' // true, same as above but quoted
.foo.x // false, "foo" doesn't contain "x"
."foo.bar" // false, property "foo.bar" doesn't exist
.foo.[0] // false, "foo" is not an array
Expand All @@ -375,7 +377,6 @@ accesses either by simply using the array index after the dot, or by wrapping it
```
Comparisons can be expressed like this:
```
.value == "bar" // false
.value == 1 // true
Expand All @@ -386,20 +387,52 @@ Comparisons can be expressed like this:
.value <= 1 // true
.string =~ /[Ss]ome/ // true
.string =~ /some/ // false
.string =~ /some/i // true, case insensitive
.string =~ "^Some" // true
.string =~ /some/i // true, case insensitive regexp
.string =~ "^Some" // true, can also use quotes
```
Conditions (evaluated left to right):
```
.foo.bar == 1 and .value == 1 // true
.foo == 1 or .bar == 2 // false
.foo == 1 or .value == 1 // true
.foo not // true if foo does not exist
.foo and .bar not // true if foo and bar don't exist together
.foo or .bar not // true if neither foo nor bar exist
.foo.bar not // true if "bar" in dictionary "foo" doesn't exist
.foo not // false, true if foo does not exist
.foo and .bar not // true, foo and bar don't exist together
.foo or .bar not // false, true if neither foo nor bar exist
.foo.bar not // false, true if "bar" in dictionary "foo" doesn't exist
```
Other:
```
near(latitude,longitude,tolerance) // true if the feature is within "tolerance" around the coordinate
```
Some complete examples:
```
// Can use single quotes for strings
mvt query -p 14_8716_8015.vector.mvt ".area > 20000 and .class == 'hospital'"
// ... or double quotes, but they must be escaped
mvt query -p 14_8716_8015.vector.mvt ".area > 20000 and .class == \"hospital\""
// No need to quote the query if it doesn't conflict with your shell
// Print all features that have an "area" property
mvt query -p 14_8716_8015.vector.mvt .area
// Features which don't have "area" and "name" properties
mvt query -p 14_8716_8015.vector.mvt .area and .name not
// Case insensitive regular expression
vt query -p 14_8716_8015.vector.mvt ".name =~ /hopital/i"
// Case sensitive regular expression
mvt query -p 14_8716_8015.vector.mvt ".name =~ /Recherches?/"
// Can also use quotes instead of slashes
mvt query -p 14_8716_8015.vector.mvt ".name =~ 'Recherches?'"
// Features around a coordinate
mvt query -p 14_8716_8015.vector.mvt "near(3.87324,11.53731,1000)"
// With other conditions
mvt query -p 14_8716_8015.vector.mvt ".name =~ /^lac/i and near(3.87324,11.53731,10000)"
```
---
Expand Down
10 changes: 10 additions & 0 deletions Sources/MVTTools/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ extension String {

var isNotEmpty: Bool { !isEmpty }

/// Trims white space and new line characters
public mutating func trim() {
self = self.trimmed()
}

/// Trims white space and new line characters, returns a new string
public func trimmed() -> String {
self.trimmingCharacters(in: .whitespacesAndNewlines)
}

func matches(_ regex: String) -> Bool {
var options: String.CompareOptions = .regularExpression

Expand Down
4 changes: 2 additions & 2 deletions Sources/MVTTools/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ extension VectorTile {
if let queryParser,
let properties = feature.properties as? [String: AnyHashable]
{
return queryParser.evaluate(on: properties)
return queryParser.evaluate(on: properties, coordinate: feature.geometry.centroid?.coordinate)
}
else {
for value in feature.properties.values.compactMap({ $0 as? String }) {
if value.contains(term) {
if value.localizedCaseInsensitiveContains(term) {
return true
}
}
Expand Down
Loading

0 comments on commit 8a4bdf7

Please sign in to comment.