Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ A Swift package for reading, writing, converting, and inspecting
[GeoJSON](https://geojson.org/),
[GPX](https://www.topografix.com/gpx.asp),
[FIT](https://developer.garmin.com/fit/overview/),
[CSV](https://en.wikipedia.org/wiki/Comma-separated_values),
[Shapefile](https://www.esri.com/content/dam/esrisites/sitecore-archive/Files/Pdfs/library/whitepapers/pdfs/shapefile.pdf),
and [GeoPackage](https://www.geopackage.org/) files.

Two products:
- **`mvt`** — CLI executable (`MVTCLI` target): dump, info, query, merge, import, export
- **`MVTTools`** — library target: `VectorTile` model, MVT/MLT encode/decode, GeoJSON/GPX/FIT/Shapefile/GeoPackage I/O, spatial queries
- **`MVTTools`** — library target: `VectorTile` model, MVT/MLT encode/decode, GeoJSON/GPX/FIT/CSV/Shapefile/GeoPackage I/O, spatial queries

Key source areas:
- **`VectorTile.swift`** — Central model: holds `[String: LayerContainer]` (layer name → features),
Expand All @@ -26,6 +27,7 @@ Key source areas:
- **`Coders/ExportOptions.swift`** — Buffer, compression, simplification options
- **`Coders/GeoJSON/VectorTile+GeoJSON.swift`** — VectorTile GeoJSON import/export init, `toGeoJson()`, `writeGeoJson()`
- **`Coders/GPX/VectorTile+GPX.swift`** — VectorTile GPX import/export init, `toGpxData()`, `writeGPX()`
- **`Coders/CSV/VectorTile+CSV.swift`** — VectorTile CSV import/export init, `toCsvData()`, `writeCSV()`
- **`Coders/FIT/VectorTile+FIT.swift`** — VectorTile FIT import/export init, `toFitData()`, `writeFIT()`
- **`Coders/Shapefile/VectorTile+Shapefile.swift`** — VectorTile Shapefile import/export init, `writeShapefile()`, `writeShapefiles()`
- **`Coders/GeoPackage/VectorTile+GeoPackage.swift`** — VectorTile GeoPackage import/export init, `writeGeoPackage()`
Expand All @@ -47,6 +49,7 @@ Projections: EPSG:4326 (WGS84), EPSG:3857 (Web Mercator), EPSG:4978 (ECEF), noSR

Dependencies:
- **GISTools** — geometry types, projections, R-Tree
- **GISToolsCSV** — CSV import/export
- **GISToolsGPX** — GPX import/export
- **GISToolsFIT** — FIT import/export
- **GISToolsShapefile** — Shapefile import/export
Expand Down
11 changes: 10 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/Outdooractive/gis-tools", from: "2.2.1"),
.package(url: "https://github.com/Outdooractive/gis-tools-csv", from: "1.0.0"),
.package(url: "https://github.com/Outdooractive/gis-tools-geopackage", from: "1.0.2"),
.package(url: "https://github.com/Outdooractive/gis-tools-gpx", from: "1.0.4"),
.package(url: "https://github.com/Outdooractive/gis-tools-fit", from: "1.0.0"),
Expand All @@ -41,6 +42,7 @@ let package = Package(
name: "MVTTools",
dependencies: [
.product(name: "GISTools", package: "gis-tools"),
.product(name: "GISToolsCSV", package: "gis-tools-csv"),
.product(name: "GISToolsGeoPackage", package: "gis-tools-geopackage"),
.product(name: "GISToolsGPX", package: "gis-tools-gpx"),
.product(name: "GISToolsFIT", package: "gis-tools-fit"),
Expand Down
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# MVTTools

MapLibre/Mapbox vector tiles (MVT/MLT) reader/writer library for Swift, together with a powerful command-line tool for working with vector tiles, GeoJSON, GPX, Shapefile, and GeoPackage files.
MapLibre/Mapbox vector tiles (MVT/MLT) reader/writer library for Swift, together with a powerful command-line tool for working with vector tiles, GeoJSON, GPX, CSV, Shapefile, and GeoPackage files.

## Table of Contents

Expand All @@ -25,6 +25,8 @@ MapLibre/Mapbox vector tiles (MVT/MLT) reader/writer library for Swift, together
- [Write MVT](#write-mvt)
- [Read GPX](#read-gpx)
- [Write GPX](#write-gpx)
- [Read CSV](#read-csv)
- [Write CSV](#write-csv)
- [Read Shapefile](#read-shapefile)
- [Write Shapefile](#write-shapefile)
- [Read GeoPackage](#read-geopackage)
Expand Down Expand Up @@ -63,6 +65,7 @@ MapLibre/Mapbox vector tiles (MVT/MLT) reader/writer library for Swift, together
- **Read & write** MapLibre/Mapbox Vector Tiles from/to disk, data objects or URLs (handles gzipped input).
- **GeoJSON import/export** — convert between MVT and GeoJSON formats.
- **GPX import/export** — convert between MVT and GPX (GPS eXchange) formats; waypoints, routes and tracks are automatically split into separate layers.
- **CSV import/export** — convert between MVT and CSV formats; a header row is required and geometry is never guessed.
- **Shapefile import/export** — convert between MVT and ESRI Shapefile format; supports single-file and per-layer directory export with mixed-geometry detection.
- **GeoPackage import/export** — convert between MVT and OGC GeoPackage format; supports single-table and per-layer export with complete roundtrip fidelity via `"gpkg_layer"` metadata.
- **Export options** — gzip compression, buffering (pixels or extents), geometry simplification (meters or extents).
Expand Down Expand Up @@ -174,6 +177,30 @@ try gpxData?.write(to: URL(fileURLWithPath: "output.gpx"))
tile.writeGPX(to: URL(fileURLWithPath: "output.gpx"))
```

### Read CSV

```swift
let csvData = try Data(contentsOf: URL(fileURLWithPath: "points.csv"))
let tile = try VectorTile(csvData: csvData)

// A header row is required; geometry is never guessed.
// Latitude/longitude columns (or a WKT "geometry" column) are used.
print(tile.layerNames) // ["Layer-0"]

// With a non-default delimiter:
let tile = try VectorTile(csvData: csvData, delimiter: ";")
```

### Write CSV

```swift
let csvData = tile.toCsvData()
try csvData?.write(to: URL(fileURLWithPath: "output.csv"))

// Or write directly
tile.writeCSV(to: URL(fileURLWithPath: "output.csv"))
```

### Read Shapefile

```swift
Expand Down Expand Up @@ -473,19 +500,19 @@ You can install the command line tool `mvt` either
- with homebrew: `brew install Outdooractive/homebrew-tap/mvt-tools`
- or locally to `/usr/local/bin` with `./install_mvt.sh`

`mvt` works with MVT, MLT, GeoJSON, GPX, Shapefile, and GeoPackage files from local disk or served from a web server.
`mvt` works with MVT, MLT, GeoJSON, GPX, CSV, Shapefile, and GeoPackage files from local disk or served from a web server.

GeoJSONs can contain a layer name in their Feature properties (default name is `vt_layer`), and any resulting GeoJSON will automatically include this property.
This can be controlled with the options `--property-name` (or `-P`), `--disable-input-layer-property` (or `-Di`) and `--disable-output-layer-property` (or `-Do`).
Some commands allow limiting the result to certain layers with `--layer` (or `-l`), which can be repeated for as many layers as necessary.

```bash
# mvt -h
OVERVIEW: A utility for inspecting and working with vector tiles (MVT/MLT), GeoJSON, GPX, Shapefile, and GeoPackage files.
OVERVIEW: A utility for inspecting and working with vector tiles (MVT/MLT), GeoJSON, GPX, FIT, CSV, Shapefile, and GeoPackage files.

A x/y/z tile coordinate is needed for encoding/decoding MVT/MLT tiles.
This tile coordinate can be extracted from the file path/URL if it's either in the form '/z/x/y' or 'z_x_y'.
Tile coordinates are not necessary for GeoJSON, GPX, Shapefile, and GeoPackage files.
Tile coordinates are not necessary for GeoJSON, GPX, FIT, CSV, Shapefile, and GeoPackage files.

Examples:
- Tests/MVTToolsTests/TestData/14_8716_8015.vector.mvt
Expand All @@ -498,7 +525,7 @@ OPTIONS:
-h, --help Show help information.

SUBCOMMANDS:
dump (default) Print the input file (MVT, MLT, GeoJSON, GPX, Shapefile, or GeoPackage) as pretty-printed GeoJSON to the console
dump (default) Print the input file (MVT, MLT, GeoJSON, GPX, FIT, CSV, Shapefile, or GeoPackage) as pretty-printed GeoJSON to the console
info Print information about the input file
query Query the features in the input file
merge Merge any number of input files into a single file of any supported format
Expand Down Expand Up @@ -679,7 +706,7 @@ mvt merge --output merged.mvt path/to/first.geojson path/to/second.geojson
mvt merge --output merged.geojson path/to/first.mvt path/to/second.mvt

# All supported formats can be mixed:
mvt merge --output merged.gpkg data.geojson tracks.gpx roads.shp tile.mvt
mvt merge --output merged.gpkg data.geojson tracks.gpx points.csv roads.shp tile.mvt
```
---
### mvt export
Expand Down
4 changes: 2 additions & 2 deletions Sources/MVTCLI/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ struct CLI: AsyncParsableCommand {

static let configuration = CommandConfiguration(
commandName: "mvt",
abstract: "A utility for inspecting and working with vector tiles (MVT/MLT), GeoJSON, GPX, FIT, Shapefile, and GeoPackage files.",
abstract: "A utility for inspecting and working with vector tiles (MVT/MLT), GeoJSON, GPX, FIT, CSV, Shapefile, and GeoPackage files.",
discussion: """
A x/y/z tile coordinate is needed for encoding/decoding MVT/MLT tiles.
This tile coordinate can be extracted from the path/URL if it's either in the form '/z/x/y' or 'z_x_y'.
Tile coordinates are not necessary for GeoJSON, GPX, FIT, Shapefile, and GeoPackage files (a tile coordinate will be calculated from the bounding box).
Tile coordinates are not necessary for GeoJSON, GPX, FIT, CSV, Shapefile, and GeoPackage files (a tile coordinate will be calculated from the bounding box).

Examples:
- Tests/MVTToolsTests/TestData/14_8716_8015.vector.mvt
Expand Down
8 changes: 4 additions & 4 deletions Sources/MVTCLI/MergeOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ extension CLI {
@Option(
name: [.short, .customLong("output")],
help: "Output file (optional, default is console).",
completion: .file(extensions: ["pbf", "mvt", "mlt", "json", "geojson", "fit", "gpx", "shp", "gpkg"]))
completion: .file(extensions: ["pbf", "mvt", "mlt", "json", "geojson", "fit", "gpx", "csv", "shp", "gpkg"]))
var outputFile: String?

@Option(
name: [.customShort("O"), .long],
help: "Output file format (optional, one of 'auto', 'geojson', 'mvt', 'mlt', 'fit', 'gpx', 'shapefile', 'geopackage').")
help: "Output file format (optional, one of 'auto', 'geojson', 'mvt', 'mlt', 'fit', 'gpx', 'csv', 'shapefile', 'geopackage').")
var outputFormat: TileFormat?

@Option(
Expand Down Expand Up @@ -98,7 +98,7 @@ extension CLI {

@Argument(
help: "Files to merge (file or URL).",
completion: .file(extensions: ["pbf", "mvt", "mlt", "geojson", "json", "fit", "gpx", "shp", "gpkg"]))
completion: .file(extensions: ["pbf", "mvt", "mlt", "geojson", "json", "fit", "gpx", "csv", "shp", "gpkg"]))
var other: [String] = []

}
Expand Down Expand Up @@ -269,7 +269,7 @@ extension CLI.MergeOptions {
}
else {
switch resolvedOutputFormat {
case .geoJson, .fit, .gpx, .shapefile, .geopackage:
case .geoJson, .fit, .gpx, .csv, .shapefile, .geopackage:
exportOptions.bufferSize = .extent(0)
default:
exportOptions.bufferSize = .extent(512)
Expand Down
19 changes: 16 additions & 3 deletions Sources/MVTCLI/TileFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum TileFormat: Equatable {
case geoJson(layerProperty: String?)
case fit
case gpx
case csv
case shapefile
case geopackage

Expand All @@ -22,6 +23,8 @@ enum TileFormat: Equatable {
self = .geoJson(layerProperty: nil)
case "gpx":
self = .gpx
case "csv":
self = .csv
case "fit":
self = .fit
case "shp":
Expand All @@ -41,6 +44,7 @@ enum TileFormat: Equatable {
case .geoJson: return ["geojson", "json"]
case .fit: return ["fit"]
case .gpx: return ["gpx"]
case .csv: return ["csv"]
case .shapefile: return ["shp"]
case .geopackage: return ["gpkg"]
}
Expand All @@ -49,15 +53,15 @@ enum TileFormat: Equatable {
/// Whether this format supports named layers on input.
var supportsInputLayers: Bool {
switch self {
case .mvt, .mlt, .geoJson, .fit, .gpx, .geopackage: true
case .mvt, .mlt, .geoJson, .fit, .gpx, .csv, .geopackage: true
case .shapefile: false
}
}

/// Whether this format can use the layer property for input routing.
var supportsInputLayerProperty: Bool {
switch self {
case .geoJson, .gpx: true
case .geoJson, .gpx, .csv: true
default: false
}
}
Expand Down Expand Up @@ -121,6 +125,13 @@ extension TileFormat {
layerAllowlist: layerAllowlist,
logger: logger)

case .csv:
return try VectorTile(
contentsOfCSV: url,
layerProperty: layerProperty,
layerAllowlist: layerAllowlist,
logger: logger)

case .shapefile:
return try VectorTile(
shapefile: url,
Expand Down Expand Up @@ -172,7 +183,7 @@ extension TileFormat {
default:
throw CLIError(
"Unrecognized file extension '.\(url.pathExtension)'. "
+ "Supported: mvt, pbf, mlt, geojson, json, fit, gpx, shp, gpkg")
+ "Supported: mvt, pbf, mlt, geojson, json, fit, gpx, csv, shp, gpkg")
}
}

Expand All @@ -189,6 +200,7 @@ extension TileFormat: CustomStringConvertible {
case .geoJson: return "geojson"
case .fit: return "fit"
case .gpx: return "gpx"
case .csv: return "csv"
case .shapefile: return "shapefile"
case .geopackage: return "geopackage"
}
Expand All @@ -208,6 +220,7 @@ extension TileFormat: ExpressibleByArgument {
case "geojson", "json": self = .geoJson(layerProperty: nil)
case "fit": self = .fit
case "gpx": self = .gpx
case "csv": self = .csv
case "shp", "shapefile": self = .shapefile
case "gpkg", "geopackage": self = .geopackage
default: return nil
Expand Down
5 changes: 5 additions & 0 deletions Sources/MVTCLI/WriteTile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ extension CLI {
throw CLIError("Failed to write GPX")
}

case .csv:
guard tile.writeCSV(to: url, options: options) else {
throw CLIError("Failed to write CSV")
}

case .shapefile:
try tile.writeShapefile(to: url, options: options)

Expand Down
Loading
Loading