Skip to content

Commit

Permalink
#33: Adds near(lat,long,tolerance) to the query language
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Sep 9, 2024
1 parent e58ddd1 commit 55c4431
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 58 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
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,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 @@ -391,7 +390,6 @@ Comparisons can be expressed like this:
```
Conditions (evaluated left to right):
```
.foo.bar == 1 and .value == 1 // true
.foo == 1 or .bar == 2 // false
Expand All @@ -402,6 +400,39 @@ Conditions (evaluated left to right):
.foo.bar not // 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)"
```
---
### mvt merge
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 55c4431

Please sign in to comment.