Skip to content

Commit

Permalink
Can parse simple jq query like strings
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Sep 6, 2024
1 parent d884b82 commit 06b8d19
Show file tree
Hide file tree
Showing 5 changed files with 571 additions and 102 deletions.
4 changes: 3 additions & 1 deletion Sources/MVTCLI/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension CLI {
var path: String

@Argument(help: "Search term, can be a string or a coordinate in the form 'latitude,longitude,tolerance(meters)'.")
var searchTerm: String
var searchTerms: [String]

mutating func run() async throws {
if let outputFile {
Expand All @@ -94,6 +94,8 @@ extension CLI {
}
}

let searchTerm = searchTerms.joined(separator: " ")

var coordinate: Coordinate3D?
var tolerance: CLLocationDistance?

Expand Down
4 changes: 3 additions & 1 deletion Sources/MVTTools/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Foundation

extension String {

public func matches(_ regex: String) -> Bool {
var isNotEmpty: Bool { !isEmpty }

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

var regex = regex
Expand Down
15 changes: 12 additions & 3 deletions Sources/MVTTools/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,24 @@ extension VectorTile {
layerNames
}

let queryParser = QueryParser(string: term)

var result: [QueryResult] = []

for layerName in queryLayerNames {
guard let layerFeatureContainer = layers[layerName] else { continue }

let resultFeatures: [Feature] = layerFeatureContainer.features.filter({ feature in
for value in feature.properties.values.compactMap({ $0 as? String }) {
if value.contains(term) {
return true
if let queryParser,
let properties = feature.properties as? [String: AnyHashable]
{
return queryParser.evaluate(on: properties)
}
else {
for value in feature.properties.values.compactMap({ $0 as? String }) {
if value.contains(term) {
return true
}
}
}

Expand Down
Loading

0 comments on commit 06b8d19

Please sign in to comment.