Skip to content

Commit

Permalink
v0.0.35
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Dec 9, 2021
1 parent aa33309 commit bab6e15
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v0.0.35
* Fixed a WKT parsing bug.

v0.0.34
* Changed tmerc to use etmerc for points >3deg from lon_0, unless the +approx flag is used.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mproj",
"version": "0.0.34",
"version": "0.0.35",
"description": "A JavaScript port of the Proj.4 cartographic projections library",
"keywords": [
"projections",
Expand Down
1 change: 1 addition & 0 deletions src/proj.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ api.internal = {
RAD_TO_DEG: RAD_TO_DEG,
wkt_parse: wkt_parse,
wkt_unpack: wkt_unpack,
convert_wkt_quotes: convert_wkt_quotes,
wkt_to_proj4: wkt_to_proj4,
wkt_from_proj4: wkt_from_proj4,
wkt_make_projcs: wkt_make_projcs,
Expand Down
22 changes: 20 additions & 2 deletions src/wkt/wkt_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ function wkt_parse(str) {
// WKT format: http://docs.opengeospatial.org/is/12-063r5/12-063r5.html#11
function wkt_unpack(str) {
var obj;
// Convert WKT escaped quote to JSON escaped quote
str = str.replace(/""/g, '\\"');
// Convert WKT escaped quotes to JSON escaped quotes
// str = str.replace(/""/g, '\\"'); // BUGGY
str = convert_wkt_quotes(str);

// Convert WKT entities to JSON arrays
str = str.replace(/([A-Z0-9]+)\[/g, '["$1",');
Expand All @@ -35,6 +36,23 @@ function wkt_unpack(str) {
return obj;
}

// Convert WKT escaped quotes to JSON escaped quotes ("" -> \")
function convert_wkt_quotes(str) {
var c = 0;
return str.replace(/"+/g, function(s) {
var even = c % 2 == 0;
c += s.length;
// ordinary, unescaped quotes
if (s == '"' || s == '""' && even) return s;
// WKT-escaped quotes
if (even) {
return '"' + s.substring(1).replace(/""/g, '\\"');
} else {
return s.replace(/""/g, '\\"');
}
});
}

// Rearrange a subarray of a parsed WKT file for easier traversal
// E.g.
// ["WGS84", ...] to {NAME: "WGS84"}
Expand Down
1 change: 1 addition & 0 deletions test/prj/issues/Anmarkningar.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223562997]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]]
21 changes: 21 additions & 0 deletions test/wkt_parse_issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ var assert = require('assert'),

describe('WKT parsing issues', function () {

it('Parsing empty string', function() {
var str = fs.readFileSync('test/prj/issues/Anmarkningar.prj', 'utf8');
var obj = api.internal.wkt_unpack(str);
assert.equal(obj[0][0], 'GEOGCS');
assert.equal(obj[0][1], '');
})

it('Unescape internal quotes', function() {
var str = `"Datum origin is 30°25'20""N, 130°25'20""E."`
var expect = `"Datum origin is 30°25'20\\"N, 130°25'20\\"E."`;
var output = api.internal.convert_wkt_quotes(str);
assert.equal(output, expect)
})

it('Unescape enclosing quotes', function() {
var str = `"""Datum origin is 30°25'20""N, 130°25'20""E."""`
var expect = `"\\"Datum origin is 30°25'20\\"N, 130°25'20\\"E.\\""`;
var output = api.internal.convert_wkt_quotes(str);
assert.equal(output, expect)
})

it('Parsing UTM 55S', function() {
var str = fs.readFileSync('test/prj/issues/papua_new_guinea.prj', 'utf8');
var proj4 = wkt_to_proj4(str);
Expand Down
1 change: 1 addition & 0 deletions test/wkt_parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assert = require('assert'),
fs = require('fs');

describe('wkt_parse.js', function() {

it('wgs84 esri', function() {
var str = fs.readFileSync('test/prj/wgs84_esri.prj', 'utf8');
var expect = {
Expand Down

0 comments on commit bab6e15

Please sign in to comment.