-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.ts
77 lines (62 loc) · 1.9 KB
/
shapes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs';
import shp from 'shpjs';
import knex from 'knex';
interface TreatedClusterInfo {
cluster_no: number;
geography: any;
county_name: string;
}
const cluster_no_property_name = 'DEM360';
const process = (fileName: string): Promise<TreatedClusterInfo[]> => {
return new Promise((resolve, reject) => {
const file = fs.readFileSync(fileName);
console.log('processing shape file');
shp(file)
.then((geojson: any) => {
console.log('processed');
const structuredResults: TreatedClusterInfo[] = geojson.features.map((result: any) => ({
cluster_no: result.properties[cluster_no_property_name],
geography: {
type: 'Feature',
id: result.properties[cluster_no_property_name],
geometry: result.geometry,
},
county_name: 'NA',
}));
console.log('total clusters: ' + structuredResults.length);
console.log('example rows', structuredResults.slice(3));
resolve(structuredResults);
})
.catch(console.error);
});
};
const upload = async (clusterInfo: TreatedClusterInfo[]) => {
const db = knex({
client: 'pg',
connection: {
host: 'cecdssdb.postgres.database.azure.com',
user: 'cec',
password: '',
database: 'cecdss',
port: 5432,
ssl: true,
},
});
console.log('connected to pg');
console.log('about to upload ' + clusterInfo.length + ' clusters');
try {
await db.transaction(async (txn) => {
await txn.table('treatedclustersInfo').truncate();
// try with batch insert
await txn.batchInsert('treatedclustersInfo', clusterInfo);
await txn.commit();
});
} catch (error) {
console.error(error);
}
console.log('insert complete');
};
process('./data/CaliforniaClusters.zip')
// process('./data/Sierra_Nevada_shapes.zip')
.then(upload)
.then(() => console.log('done'));