Skip to content

Instantly share code, notes, and snippets.

@yurukov
Last active February 11, 2023 13:35
Show Gist options
  • Save yurukov/e80f865a7a803e7c01378441f8a07556 to your computer and use it in GitHub Desktop.
Save yurukov/e80f865a7a803e7c01378441f8a07556 to your computer and use it in GitHub Desktop.
Transforms EPSG8122 to WGS84, reduces the precision and simplifies the structure if size is too big

Requires proj4 and simplify-geojson. Receives coordinates in EPSG:8122 projection as regularly used in Bulgarian arcgeo government servers. Transforms to geojson format with reduced precision of less than a meter. Then if the size of the resulting file is bigger than 20k characters, it simplifies it to reduce load on processing later on.

data = process.argv[2];
header=data.substr(0,data.indexOf("(")).toUpperCase().trim();
if (header=="POLYGON" || header=="POINT" || header=="MULTIPOINT" || header=="MULTIPOLYGON" || header=="MULTILINESTRING" || header=="LINESTRING") {
var proj4 = require("proj4");
proj4.defs("EPSG:8122",'PROJCS["BGS2005", GEOGCS["GCS_WGS_1984", DATUM["D_WGS_1984", SPHEROID["WGS_1984", 6378137.0, 298.257223563]], PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], AXIS["Longitude", EAST], AXIS["Latitude", NORTH]], PROJECTION["Lambert_Conformal_Conic_2SP"], PARAMETER["central_meridian", 25.5], PARAMETER["latitude_of_origin", 42.66787568333], PARAMETER["standard_parallel_1", 43.33333333333], PARAMETER["false_easting", 500000.0], PARAMETER["false_northing", 4725824.3591], PARAMETER["scale_factor", 1.0], PARAMETER["standard_parallel_2", 42.0], UNIT["m", 1.0], AXIS["x", EAST], AXIS["y", NORTH], AUTHORITY["EPSG","8122"]]');
if (header=="POLYGON")
header="Polygon";
else if (header=="POINT")
header="Point";
else if (header=="MULTIPOINT")
header="MultiPoint";
else if (header=="MULTIPOLYGON")
header="MultiPolygon";
else if (header=="MULTILINESTRING")
header="MultiLineString";
else if (header=="LINESTRING")
header="LineString";
res=[];
data=data.substring(data.indexOf("("));
data=data.replace(/\(/g,"[").replace(/\)/g,"]").replace(new RegExp("([0-9]\+\\.?[0-9]*) ([0-9]\+\\.?[0-9]*)", 'g'), function (match) {
var d=match.split(" ");
d[0]=parseFloat(d[0]);
d[1]=parseFloat(d[1]);
d=proj4('EPSG:8122').inverse(d);
d[0]=d[0].toFixed(6);
d[1]=d[1].toFixed(6);
return "["+d.join(",")+"]";
});
geo='{"type":"Feature","properties":{},"geometry":{"type":"'+header+'","coordinates":'+data+'}}';
if (geo.length>20000) {
var simplify = require('simplify-geojson')
geo=simplify(JSON.parse(geo),0.00005);
geo=JSON.stringify(geo);
}
console.log(geo);
} else {
console.log("ERROR");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment