UNPKG

5.1 kBPlain TextView Raw
1import test from "blue-tape"
2import {
3 createReadStream,
4 createWriteStream,
5 ReadStream,
6 statSync,
7 writeFileSync,
8} from "fs"
9import ogr2ogr from "./"
10
11let dir = __dirname + "/testdata/"
12
13test(async (t) => {
14 const vers = await ogr2ogr.version()
15 t.match(vers, /^GDAL /)
16
17 interface TT {
18 file?: string
19 url?: string
20 out?: string
21 opts?: string[]
22 dest?: string
23 stream?: boolean
24 success: boolean
25 }
26 let table: TT[] = [
27 // URL tests.
28 {
29 url: "https://gist.github.com/wavded/7376428/raw/971548233e441615a426794c766223488492ddb9/test.geojson",
30 success: true,
31 },
32 {
33 url: "https://gist.github.com/wavded/7376428/raw/971548233e441615a426794c766223488492ddb9/test.georss",
34 success: true,
35 },
36
37 // From format conversions.
38 {file: "sample.bad", success: false},
39 {file: "sample.empty.zip", success: false},
40 {file: "sample.000", success: true},
41 {file: "sample.csv", success: true},
42 {file: "sample.dbf", success: true},
43 {file: "sample.dgn", success: true},
44 {file: "sample.dxf", success: true},
45 {file: "sample.gdb.zip", out: "dxf", success: true},
46 {file: "sample.geojson", success: true},
47 {file: "sample.gml", success: true},
48 {file: "sample.gmt", success: true},
49 {file: "sample.gxt", success: true},
50 {file: "sample.itf", success: true},
51 {file: "sample.json", success: true},
52 {file: "sample.jml", stream: true, success: true},
53 {file: "sample.kml", success: true},
54 {file: "sample.kmz", success: true},
55 {file: "sample.cryllic.kml", success: true},
56 {file: "sample.map.zip", success: true},
57 {file: "sample.mapml", stream: true, success: true},
58 {file: "sample.rss", success: true},
59 {file: "sample.rti.zip", out: "dxf", success: true},
60 {file: "sample.shp", success: true},
61 {file: "sample.shp.zip", success: true},
62 {file: "sample.large.shp.zip", success: true},
63 {file: "sample.vdv", stream: true, success: true},
64
65 // Using custom options.
66 {
67 file: "sample.no-shx.shp",
68 opts: ["--config", "SHAPE_RESTORE_SHX", "TRUE"],
69 success: true,
70 },
71 {
72 file: "sample.geom.csv",
73 opts: ["-oo", "GEOM_POSSIBLE_NAMES=the_geom"],
74 success: true,
75 },
76
77 // To format conversions.
78 {file: "sample.json", success: true, out: "csv"},
79 {file: "sample.json", success: true, out: "dgn"},
80 {file: "sample.json", success: true, out: "dxf"},
81 {file: "sample.json", success: true, out: "esri shapefile"},
82 {file: "sample.json", success: true, out: "flatgeobuf"},
83 {file: "sample.json", success: true, out: "geoconcept"},
84 {file: "sample.json", success: true, out: "geojson"},
85 {file: "sample.json", success: true, out: "geojsonseq"},
86 {file: "sample.json", success: true, out: "georss"},
87 {file: "sample.json", success: true, out: "gml"},
88 {file: "sample.json", success: true, out: "gmt"},
89 {file: "sample.json", success: true, out: "gpkg"},
90 {file: "sample.json", success: true, out: "gpx"},
91 {file: "sample.json", success: true, out: "jml"},
92 {file: "sample.json", success: true, out: "kml"},
93 {file: "sample.json", success: true, out: "mapml"},
94 {file: "sample.json", success: true, out: "mapinfo file"},
95 {file: "sample.json", success: true, out: "ods"},
96 {file: "sample.json", success: true, out: "pdf"},
97 {file: "sample.json", success: true, out: "vdv"},
98 {file: "sample.json", success: true, out: "xlsx"},
99
100 // Known supported stream conversions.
101 {file: "sample.csv", stream: true, success: false},
102 {file: "sample.json", stream: true, success: true},
103 {file: "sample.rss", stream: true, success: true},
104 {file: "sample.gml", stream: true, success: true},
105 {file: "sample.gmt", stream: true, success: true},
106 {file: "sample.gpx", stream: true, success: true},
107 {file: "sample.jml", stream: true, success: true},
108 {file: "sample.kml", stream: true, success: true},
109 {file: "sample.mapml", stream: true, success: true},
110 {file: "sample.vdv", stream: true, success: true},
111
112 // Custom destinations. (e.g. database)
113 {
114 file: "sample.json",
115 success: true,
116 dest: dir + "output/custom.geojson",
117 },
118 ]
119
120 for (let tt of table) {
121 try {
122 let input: string | ReadStream = tt.url ? tt.url : dir + tt.file
123 if (tt.stream) {
124 input = createReadStream(input)
125 }
126
127 let res = await ogr2ogr(input, {
128 format: tt.out,
129 options: tt.opts,
130 destination: tt.dest,
131 maxBuffer: 1024 * 1024 * 1024,
132 })
133
134 if (tt.dest) {
135 statSync(tt.dest)
136 t.pass()
137 } else if (!tt.out) {
138 t.equal(res.data && res.data.type, "FeatureCollection", res.cmd)
139 } else {
140 t.ok(res.text || res.stream, res.cmd)
141
142 let fn = dir + "output/r_" + tt.out + res.extname
143 if (res.stream) {
144 res.stream.pipe(createWriteStream(fn))
145 } else {
146 writeFileSync(fn, res.text)
147 }
148 }
149 t.ok(tt.success)
150 } catch (err) {
151 console.log(err)
152 t.notOk(tt.success)
153 }
154 }
155})