4
4
"crypto/sha256"
5
5
"embed"
6
6
"encoding/json"
7
+ "errors"
7
8
"fmt"
8
9
"log"
9
10
"os"
@@ -12,9 +13,12 @@ import (
12
13
13
14
"github.com/blevesearch/bleve/v2"
14
15
"github.com/everystreet/go-shapefile"
16
+ "github.com/mitchellh/go-homedir"
15
17
"github.com/twpayne/go-geom"
16
18
)
17
19
20
+ var DATABASE_PATH string = filepath .Join (CreateConfigDir (), "mada.bleve" )
21
+
18
22
type Shape struct {
19
23
Type string `json:"type"`
20
24
Bbox []float32 `json:"bbox"`
@@ -56,7 +60,7 @@ type Geometry struct {
56
60
//go:embed shp/*
57
61
var Assets embed.FS
58
62
59
- func Init () {
63
+ func Init () (bleve. Index , error ) {
60
64
61
65
index , err := CreateOrOpenBleve ()
62
66
@@ -82,16 +86,18 @@ func Init() {
82
86
parseShapefile (filename , index )
83
87
}
84
88
89
+ return index , nil
85
90
}
86
91
87
92
func CreateOrOpenBleve () (bleve.Index , error ) {
88
- if _ , err := os .Stat ("mada.bleve" ); os .IsNotExist (err ) {
93
+ if _ , err := os .Stat (DATABASE_PATH ); os .IsNotExist (err ) {
89
94
geometryMapping := bleve .NewDocumentDisabledMapping ()
95
+
90
96
mapping := bleve .NewIndexMapping ()
91
97
mapping .DefaultMapping .AddSubDocumentMapping ("geometry" , geometryMapping )
92
- return bleve .New ("mada.bleve" , mapping )
98
+ return bleve .New (DATABASE_PATH , mapping )
93
99
}
94
- return bleve .Open ("mada.bleve" )
100
+ return bleve .Open (DATABASE_PATH )
95
101
}
96
102
97
103
func parseShapefile (name string , index bleve.Index ) {
@@ -192,3 +198,15 @@ func parseShapefile(name string, index bleve.Index) {
192
198
// Err() returns the first error encountered during calls to Record()
193
199
scanner .Err ()
194
200
}
201
+
202
+ func CreateConfigDir () string {
203
+ home , _ := homedir .Dir ()
204
+ path := filepath .Join (home , ".mada" )
205
+ if _ , err := os .Stat (path ); errors .Is (err , os .ErrNotExist ) {
206
+ err := os .Mkdir (path , os .ModePerm )
207
+ if err != nil {
208
+ panic (err )
209
+ }
210
+ }
211
+ return path
212
+ }
0 commit comments