Order geo-columns to export 'the_geom' by default while exporting SHP format

This commit is contained in:
Daniel García Aubert 2017-11-17 14:45:58 +01:00
parent 2211e7c0ed
commit 833b7b962c

View File

@ -72,10 +72,19 @@ OgrFormat.prototype.toOGR = function(options, out_format, out_filename, callback
var columns = []; var columns = [];
var geocol; var geocol;
var pg; var pg;
// Drop ending semicolon (ogr doens't like it) // Drop ending semicolon (ogr doens't like it)
sql = sql.replace(/;\s*$/, ''); sql = sql.replace(/;\s*$/, '');
const theGeomFirst = (fieldA, fieldB) => {
if (fieldA.name === 'the_geom') {
return -1;
}
if (fieldB.name === 'the_geom') {
return 1;
}
return 0;
};
step ( step (
function fetchColumns() { function fetchColumns() {
@ -83,37 +92,26 @@ OgrFormat.prototype.toOGR = function(options, out_format, out_filename, callback
pg = new PSQL(dbopts); pg = new PSQL(dbopts);
pg.query(colsql, this); pg.query(colsql, this);
}, },
// jshint maxcomplexity:9
function findSRS(err, result) { function findSRS(err, result) {
assert.ifError(err); assert.ifError(err);
//if ( ! result.rows.length ) throw new Error("Query returns no rows");
var needSRS = that._needSRS; var needSRS = that._needSRS;
// Skip system columns, find geom column columns = result.fields
for (var i=0; i<result.fields.length; ++i) { // skip columns
var field = result.fields[i]; .filter(field => skipfields.indexOf(field.name) === -1)
var k = field.name; // put "the_geom" first (if exists)
if ( skipfields.indexOf(k) !== -1 ) { .sort(theGeomFirst)
continue; // get first geometry to calculate SRID ("the_geom" if exists)
} .map(field => {
if ( out_format !== 'CSV' && k === "the_geom_webmercator" ) { if (needSRS && !geocol && pg.typeName(field.dataTypeID) === 'geometry') {
continue; geocol = field.name;
} // TODO: drop ?
if ( out_format === 'CSV' ) {
columns.push(pg.quoteIdentifier(k)+'::text');
} else {
columns.push(pg.quoteIdentifier(k));
} }
if ( needSRS ) { return field;
if ( ! geocol && pg.typeName(field.dataTypeID) === 'geometry' ) { })
geocol = k; // apply quotes to columns
} .map(field => out_format === 'CSV' ? pg.quoteIdentifier(field.name)+'::text' : pg.quoteIdentifier(field.name));
}
}
//console.log(columns.join(','));
if ( ! needSRS || ! geocol ) { if ( ! needSRS || ! geocol ) {
return null; return null;
@ -137,7 +135,6 @@ OgrFormat.prototype.toOGR = function(options, out_format, out_filename, callback
next(null); next(null);
} }
}); });
}, },
function spawnDumper(err, srid, type) { function spawnDumper(err, srid, type) {
assert.ifError(err); assert.ifError(err);