Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Andykmcc committed Jun 11, 2024
1 parent 4fa5722 commit db2513c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extracts": [
{
"output": "san-francisco-bay-area.osm.pbf",
"directory": "./north-america/us/california",
"description": "San Francisco Bay Area nine counties.",
"polygon": {
"file_name": "san-francisco-bay-area-convex.geojson",
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module osm-extrator/main

go 1.22.0
Binary file added main
Binary file not shown.
98 changes: 98 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
)

type Extracts struct {
Extracts []Extract `json:"Extracts"`
}

type Extract struct {
Output string `json:"output"`
Directory string `json:"directory"`
Description string `json:"description"`
Polygon polygon `json:"polygon"`
}

type polygon struct {
FileName string `json:"file_name"`
FileType string `json:"file_type"`
}

func osmium_extract() {
cmd := exec.Command("osmium", "extract", "-d", "./volumes/output", "-c", "./config.json", "./volumes/input/latest.osm.pbf")
stderr, err := cmd.StderrPipe()
if err != nil {
log.Print("error getting osmium standerr pipe")
}
err = cmd.Start()
if err != nil {
log.Printf("Osmium extract cmd.Start() failed with %s\n", err)
log.Fatal(err)
}

stderrin := bufio.NewScanner(stderr)
for stderrin.Scan() {
fmt.Println(stderrin.Text())
}
err = cmd.Wait()
if err != nil {
log.Printf("Osmium failed: %s\n", err)
log.Fatal(err)
}
}

func move_extracts(src string, dest string) {
err := os.MkdirAll(filepath.Dir(dest), os.ModePerm)
if err != nil {
log.Fatal(err)
}
err = os.Rename(src, dest)
if err != nil {
log.Fatal(err)
}
}

func main() {
osmium_extract()

jsonFile, err := os.Open("config.json")

if err != nil {
log.Fatalf("Failed to read config JSON: %s\n", err)
}

defer jsonFile.Close()

byteValue, _ := io.ReadAll(jsonFile)

var extracts Extracts

err = json.Unmarshal(byteValue, &extracts)
if err != nil {
log.Fatalf("Error nnmarshaling JSON: %s\n", err)
}

currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

for i := 0; i < len(extracts.Extracts); i++ {
sourcePath := filepath.Join(currentDir, extracts.Extracts[i].Output)
destDir := filepath.Join(currentDir, extracts.Extracts[i].Directory)
destPath := filepath.Join(destDir, extracts.Extracts[i].Output)

move_extracts(sourcePath, destPath)

fmt.Println("File moved successfully.")
}
}

0 comments on commit db2513c

Please sign in to comment.