From 0d84a704df53df8d85aa39ed2dd7f89199d2cd5d Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 1 Oct 2013 17:19:44 +0200 Subject: [PATCH] Fix missing .prj file in shapefile format Finds srid, when needed, with an additional query. Closes #110. Includes testcases. --- NEWS.md | 1 + app/models/formats/ogr.js | 79 ++++++++++++++++++++--------- app/models/formats/shp.js | 1 + app/models/psql.js | 29 ++++++++++- test/acceptance/export/shapefile.js | 25 +++++---- test/test.sql | 4 +- 6 files changed, 102 insertions(+), 37 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4bed88fd..c8cd0412 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ 1.6.0 ----- +* Fix missing .prj in shapefile export (#110) * Improve recognition of non-standard field types names by db lookup (#112) 1.5.4 - 2013-10-01 diff --git a/app/models/formats/ogr.js b/app/models/formats/ogr.js index 31c0803e..3d26f9a6 100644 --- a/app/models/formats/ogr.js +++ b/app/models/formats/ogr.js @@ -65,7 +65,11 @@ ogr.prototype.toOGR = function(dbname, user_id, gcol, sql, skipfields, out_forma var dbuser = userid_to_dbuser(user_id); var dbpass = ''; // turn into a parameter.. + var that = this; + var columns = []; + var geocol; + var pg; // Drop ending semicolon (ogr doens't like it) sql = sql.replace(/;\s*$/, ''); @@ -73,32 +77,60 @@ ogr.prototype.toOGR = function(dbname, user_id, gcol, sql, skipfields, out_forma Step ( function fetchColumns() { - var colsql = 'SELECT * FROM (' + sql + ') as _cartodbsqlapi LIMIT 1'; - var pg = new PSQL(user_id, dbname, 1, 0); + var colsql = 'SELECT * FROM (' + sql + ') as _cartodbsqlapi LIMIT 0'; + pg = new PSQL(user_id, dbname, 1, 0); pg.query(colsql, this); }, - function spawnDumper(err, result) { + function findSRS(err, result) { if (err) throw err; //if ( ! result.rows.length ) throw new Error("Query returns no rows"); - // Skip system columns - if ( result.rows.length ) { - for (var k in result.rows[0]) { - if ( skipfields.indexOf(k) != -1 ) continue; - if ( out_format != 'CSV' && k == "the_geom_webmercator" ) continue; // TODO: drop ? - if ( out_format == 'CSV' ) columns.push('"' + k + '"::text'); - else columns.push('"' + k + '"'); + var needSRS = that._needSRS; + + // Skip system columns, find geom column + for (var i=0; i