Repository navigation
Expand file tree
/
Copy pathtest-asm.js
More file actions
102 lines (86 loc) · 2.81 KB
/
Copy pathtest-asm.js
File metadata and controls
102 lines (86 loc) · 2.81 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import https from 'https';
import http from 'http';
async function testASM() {
try {
console.log('Testing ASM in Node.js...');
// Test the meta API
const metaUrl = 'https://www.geoboundaries.org/api/current/gbOpen/ASM/ADM0/';
console.log('Fetching meta:', metaUrl);
const metaData = await fetchJson(metaUrl);
console.log('Meta response:', metaData);
const dl = metaData.gjDownloadURL || metaData.simplifiedGeometryGeoJSON ||
metaData.geojsonDownloadURL || metaData.geojsonURL ||
metaData.downloadURL || metaData.tjDownloadURL;
console.log('Download URL:', dl);
if (!dl) {
throw new Error('No download URL found');
}
// Test the GeoJSON download
console.log('Fetching GeoJSON...');
const geoJsonData = await fetchJsonRaw(dl);
console.log('Raw response length:', geoJsonData.length);
console.log('Raw response starts with:', geoJsonData.substring(0, 200));
console.log('Raw response ends with:', geoJsonData.substring(geoJsonData.length - 200));
// Try to parse as JSON
try {
const parsed = JSON.parse(geoJsonData);
console.log('GeoJSON type:', parsed.type);
if (parsed.type === 'Topology') {
console.log('Topology keys:', Object.keys(parsed.objects));
} else if (parsed.type === 'FeatureCollection') {
console.log('Features count:', parsed.features?.length);
}
} catch (parseError) {
console.error('JSON parse error:', parseError.message);
}
} catch (error) {
console.error('Error:', error.message);
}
}
function fetchJson(url) {
return new Promise((resolve, reject) => {
const protocol = url.startsWith('https') ? https : http;
protocol.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(new Error('Invalid JSON'));
}
});
}).on('error', (err) => {
reject(err);
});
});
}
function fetchJsonRaw(url) {
return new Promise((resolve, reject) => {
const protocol = url.startsWith('https') ? https : http;
const req = protocol.get(url, (res) => {
console.log('HTTP Status:', res.statusCode);
console.log('Response headers:', res.headers);
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Total data length received:', data.length);
resolve(data);
});
});
req.on('error', (err) => {
console.error('Request error:', err);
reject(err);
});
req.setTimeout(10000, () => {
console.log('Request timed out');
req.destroy();
reject(new Error('Request timeout'));
});
});
}
testASM();