-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
8,355 additions
and
3,581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ArcCollection } from '../paths/mapshaper-arcs'; | ||
import { DataTable } from '../datatable/mapshaper-data-table'; | ||
|
||
export function exportBSON(datasets, opts) { | ||
var { serialize } = require('bson'); | ||
var obj = exportDatasets(datasets); | ||
var content = serialize(obj); | ||
return [{ | ||
content: content, | ||
filename: opts.file || 'output.mshp' | ||
}]; | ||
} | ||
|
||
// gui: (optional) gui instance | ||
// | ||
export function exportDatasets(datasets) { | ||
// TODO: add targets | ||
// TODO: add gui state | ||
return { | ||
version: 1, | ||
datasets: datasets.map(exportDataset) | ||
}; | ||
} | ||
|
||
// TODO.. | ||
// export function serializeSession(catalog) { | ||
// var obj = exportDatasets(catalog.getDatasets()); | ||
// return BSON.serialize(obj); | ||
// } | ||
|
||
export function exportDataset(dataset) { | ||
return Object.assign(dataset, { | ||
arcs: dataset.arcs ? exportArcs(dataset.arcs) : null, | ||
info: dataset.info ? exportInfo(dataset.info) : null, | ||
layers: (dataset.layers || []).map(exportLayer) | ||
}); | ||
} | ||
|
||
function typedArrayToBuffer(arr) { | ||
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength); | ||
} | ||
|
||
function exportArcs(arcs) { | ||
var data = arcs.getVertexData(); | ||
var obj = { | ||
nn: typedArrayToBuffer(data.nn), | ||
xx: typedArrayToBuffer(data.xx), | ||
yy: typedArrayToBuffer(data.yy) | ||
}; | ||
return obj; | ||
} | ||
|
||
function exportLayer(lyr) { | ||
return { | ||
name: lyr.name || null, | ||
geometry_type: lyr.geometry_type || null, | ||
shapes: lyr.shapes || null, | ||
data: lyr.data ? lyr.data.getRecords() : null | ||
}; | ||
} | ||
|
||
function exportInfo(info) { | ||
// TODO: export CRS | ||
return info; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ArcCollection } from '../paths/mapshaper-arcs'; | ||
import { DataTable } from '../datatable/mapshaper-data-table'; | ||
import { stop } from '../utils/mapshaper-logging'; | ||
import { BinArray } from '../utils/mapshaper-binarray'; | ||
|
||
// Import datasets contained in a BSON blob | ||
// Return command target as a dataset | ||
// | ||
export function importBSON(buf) { | ||
var { deserialize } = require('bson'); | ||
var obj = deserialize(buf, { | ||
promoteBuffers: true, | ||
promoteValues: true | ||
}); | ||
if (!isValidSession(obj)) { | ||
stop('Invalid mapshaper session data object'); | ||
} | ||
|
||
var datasets = obj.datasets.map(importDataset); | ||
var target = datasets[0]; // TODO: improve | ||
return { | ||
datasets: datasets, | ||
target: target | ||
}; | ||
} | ||
|
||
function isValidSession(obj) { | ||
if (!Array.isArray(obj.datasets)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function importDataset(obj) { | ||
return { | ||
info: obj.info, | ||
layers: (obj.layers || []).map(importLayer), | ||
arcs: obj.arcs ? importArcs(obj.arcs) : null | ||
}; | ||
} | ||
|
||
function bufferToDataView(buf, constructor) { | ||
return new constructor(BinArray.copyToArrayBuffer(buf)); | ||
// this doesn't work: "RangeError: start offset of Float64Array should be a multiple of 8" | ||
// return new constructor(buf.buffer, buf.byteOffset, buf.byteLength); | ||
} | ||
|
||
function importArcs(obj) { | ||
var nn = bufferToDataView(obj.nn, Uint32Array); | ||
var xx = bufferToDataView(obj.xx, Float64Array); | ||
var yy = bufferToDataView(obj.yy, Float64Array); | ||
var arcs = new ArcCollection(nn, xx, yy); | ||
return arcs; | ||
} | ||
|
||
function importLayer(lyr) { | ||
return Object.assign(lyr, { | ||
data: lyr.data ? new DataTable(lyr.data) : null | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.