This is a fork of jczaplew/geojson-precision. This fork was created to extend the module to include more features that fulfill our particular needs. Please use jczaplew's module if this isn't the right module for you.
- Limit decimal places for all coordinates of various GeoJSON objects. This may be very handy at times. For our projects, computer generated coordinates with long unnecessary decimal points were taking too much database space (plus the bandwidth expense)!
- Reduce the number of points using the Ramer–Douglas–Peucker algorithm. Sometimes, we do not need highly detailed geographic representations. It may be useful to reduce the resolution by reducing closely clustered points.
npm install [-g] geojson-mend
###.nDecimals(geojson, precision)
This API limits the number of decimal places for all coordinates. geojson
is a valid GeoJSON object, and can be of type Point
, LineString
, Polygon
, MultiPoint
, MultiPolygon
, MultiLineString
, GeometryCollection
, Feature
, or FeatureCollection
. Invalid types are returned without any changes.
precision
is a positive integer. It specifies the maximum number of decimal places you want to allow.
Here's a simple table that summarizes relationship between the number of decimal places and distance. Use the table to decide how many decimal places you'll need for your task.
decimal
places degrees distance
------- ------- --------
0 1 111 km
1 0.1 11.1 km
2 0.01 1.11 km
3 0.001 111 m
4 0.0001 11.1 m
5 0.00001 1.11 m
6 0.000001 11.1 cm
7 0.0000001 1.11 cm
8 0.00000001 1.11 mm
var geomend = require("geojson-mend");
var isMended = geomend.nDecimals({
"type": "Point",
"coordinates": [
"20.895465",
54.98754341564654654
]
}, 4);
console.log(isMended);
Output >
{
"type": "Point",
"coordinates": [
20.8954,
54.9875
]
}
###.nReduce(geojson, tolerance)
geojson
can be of type LineString
, Polygon
, MultiPoint
, MultiPolygon
, MultiLineString
, GeometryCollection
, Feature
, or FeatureCollection
. Invalid types are returned without any changes.
Tolerance corresponds to how much resolution you can spare. Higher the tolerance, lower is your geometry's resolution. Tolerance is specified in degrees. The above table applies here too. The table gives a general idea on what tolerance to use for your task. For example, if you can allow an error of 1.11km (for example while plotting a big city map) set your tolerance to 0.01.
decimal
places degrees distance
------- ------- --------
0 1 111 km
1 0.1 11.1 km
2 0.01 1.11 km
3 0.001 111 m
4 0.0001 11.1 m
5 0.00001 1.11 m
6 0.000001 11.1 cm
7 0.0000001 1.11 cm
8 0.00000001 1.11 mm
var geomend = require("geojson-mend");
var isMended = geomend.nReduce({
"type": "Polygon",
"coordinates": [[[85.26106,27.77924],[85.2701,27.77869],[85.27235,27.78012],[85.27461,27.78185],[85.27687,27.78358],[85.27977,27.78617],[85.28365,27.78991],[85.28687,27.79365],[85.28976,27.79681],[85.29235,27.79997],[85.29525,27.80314],[85.29816,27.80659],[85.30074,27.80946],[85.30396,27.81148],[85.30816,27.81292],[85.31139,27.8135],[85.31655,27.81322],[85.32172,27.81323],[85.32462,27.81324],[85.3272,27.81324],[85.32882,27.81324],[85.33334,27.81267],[85.3356,27.81267],[85.3385,27.81239],[85.34141,27.81211],[85.34528,27.81182],[85.34722,27.81154],[85.3498,27.81154],[85.35658,27.81155],[85.35852,27.81156],[85.36078,27.81128],[85.36369,27.81099],[85.36853,27.81071],[85.37144,27.80985],[85.37337,27.80986],[85.38047,27.80958],[85.38338,27.81044],[85.3866,27.81159],[85.3908,27.81361],[85.3921,27.81418],[85.39532,27.81505],[85.4034,27.81449],[85.40727,27.81305],[85.4105,27.81248],[85.4147,27.8122],[85.41792,27.81191],[85.42244,27.81163],[85.42599,27.81134],[85.43084,27.81134],[85.43536,27.81164],[85.43923,27.81164],[85.44343,27.81222],[85.44569,27.81279],[85.44859,27.81337],[85.45021,27.81423],[85.4515,27.81681],[85.4557,27.81596],[85.45892,27.81452],[85.46054,27.81337],[85.46312,27.8105],[85.46506,27.80706],[85.46602,27.80247],[85.46666,27.79845],[85.4673,27.79443],[85.46924,27.78754],[85.47182,27.78409],[85.47408,27.78295],[85.47762,27.78295],[85.4815,27.78295],[85.48569,27.78238],[85.48795,27.78152],[85.48988,27.78094],[85.49343,27.7798],[85.49634,27.77808],[85.4986,27.77664],[85.49957,27.77521],[85.50085,27.77406],[85.50278,27.7686],[85.50375,27.76516],[85.50407,27.76085],[85.50374,27.75712],[85.50244,27.75195],[85.50114,27.74821],[85.49985,27.74448],[85.50049,27.74046],[85.50243,27.73731],[85.50597,27.73415],[85.50887,27.73329],[85.51306,27.73214],[85.51758,27.73099],[85.52016,27.73013],[85.52306,27.72898],[85.52435,27.72812],[85.51757,27.72668],[85.51596,27.7261],[85.51305,27.72553],[85.51176,27.72525],[85.50822,27.72438],[85.5066,27.72409],[85.50434,27.72409],[85.50208,27.7241],[85.4995,27.72381],[85.49725,27.72323],[85.49531,27.72295],[85.49273,27.72237],[85.49047,27.72179],[85.48853,27.7215],[85.4866,27.72093],[85.48402,27.72064],[85.48047,27.72007],[85.47756,27.71978],[85.47627,27.71949],[85.4695,27.72063],[85.46853,27.7215],[85.46498,27.72035],[85.46175,27.71977],[85.46014,27.71977],[85.45821,27.72064],[85.45692,27.72092],[85.45499,27.72178],[85.45112,27.72436],[85.44853,27.72522],[85.44595,27.72608],[85.44305,27.72637],[85.44015,27.72694],[85.43725,27.72694],[85.43466,27.72664],[85.43111,27.72521],[85.4266,27.72291],[85.42499,27.72262],[85.42176,27.72147],[85.41853,27.72089],[85.41724,27.7209],[85.41531,27.71975],[85.41434,27.71715],[85.41369,27.71486],[85.41079,27.71169],[85.40691,27.70883],[85.4053,27.70796],[85.40434,27.7071],[85.40176,27.70508],[85.39885,27.70307],[85.39756,27.70221],[85.39304,27.70049],[85.39111,27.69962],[85.38821,27.6979],[85.38401,27.69531],[85.38305,27.69473],[85.37917,27.69214],[85.37724,27.69128],[85.37563,27.69099],[85.37305,27.69041],[85.37047,27.68927],[85.36886,27.6884],[85.36725,27.68725],[85.36563,27.68524],[85.36337,27.68265],[85.36241,27.68121],[85.36079,27.67805],[85.36079,27.6769],[85.36015,27.67489],[85.35983,27.67288],[85.3595,27.67173],[85.35822,27.66887],[85.35435,27.66742],[85.35177,27.66713],[85.34951,27.66713],[85.34758,27.6677],[85.34564,27.66942],[85.34435,27.67086],[85.34306,27.672],[85.3392,27.67458],[85.33726,27.6763],[85.33629,27.67716],[85.33403,27.67916],[85.33274,27.68031],[85.33113,27.68175],[85.32952,27.6826],[85.32887,27.68318],[85.325,27.68633],[85.32275,27.68748],[85.31952,27.68891],[85.31597,27.69005],[85.31371,27.69062],[85.3092,27.69062],[85.30694,27.69061],[85.30566,27.69032],[85.30404,27.68974],[85.30017,27.68629],[85.29953,27.68542],[85.29921,27.68428],[85.29824,27.68112],[85.29792,27.67997],[85.29728,27.67652],[85.29631,27.67394],[85.2947,27.67164],[85.29438,27.66934],[85.29471,27.66704],[85.29567,27.66532],[85.29632,27.66389],[85.29664,27.66274],[85.29632,27.65987],[85.29504,27.65786],[85.29278,27.65556],[85.29052,27.65354],[85.28924,27.65182],[85.28859,27.65067],[85.28763,27.64924],[85.28666,27.64751],[85.28602,27.64635],[85.28441,27.64435],[85.28377,27.64004],[85.28377,27.63918],[85.28474,27.63717],[85.28603,27.63574],[85.28732,27.63488],[85.2886,27.63402],[85.29279,27.63087],[85.29376,27.62915],[85.29473,27.62628],[85.29505,27.62513],[85.29441,27.62197],[85.29409,27.61938],[85.29345,27.61623],[85.29281,27.61278],[85.29217,27.61048],[85.29314,27.60847],[85.29378,27.60732],[85.2941,27.60618],[85.29411,27.60531],[85.29249,27.60416],[85.29153,27.60244],[85.29056,27.60043],[85.28927,27.59842],[85.28799,27.59698],[85.28541,27.59612],[85.28283,27.59524],[85.28187,27.59266],[85.28155,27.59065],[85.28091,27.58864],[85.28059,27.58663],[85.27994,27.58548],[85.2793,27.58347],[85.27866,27.58232],[85.27705,27.5806],[85.27544,27.57945],[85.27447,27.57829],[85.27286,27.57714],[85.2719,27.57657],[85.27093,27.5757],[85.26996,27.57513],[85.26771,27.57282],[85.26706,27.57627],[85.26416,27.5777],[85.26094,27.57798],[85.25707,27.57884],[85.2532,27.57911],[85.24837,27.57968],[85.24354,27.58139],[85.24,27.58253],[85.23645,27.58396],[85.23258,27.58597],[85.23064,27.58854],[85.23129,27.59141],[85.23418,27.59516],[85.2374,27.59774],[85.23997,27.60148],[85.24094,27.60378],[85.24061,27.6101],[85.23931,27.61526],[85.2377,27.61928],[85.23512,27.62301],[85.2335,27.62444],[85.22899,27.625],[85.22544,27.62499],[85.21964,27.62526],[85.21449,27.62583],[85.21062,27.62668],[85.20772,27.62783],[85.20642,27.63012],[85.20835,27.633],[85.21222,27.63444],[85.21512,27.63617],[85.21737,27.63904],[85.21833,27.64077],[85.2193,27.64221],[85.22187,27.64623],[85.22477,27.64882],[85.22605,27.6514],[85.22734,27.65343],[85.22701,27.65658],[85.22572,27.65715],[85.22314,27.658],[85.21992,27.65915],[85.21702,27.66029],[85.2125,27.66229],[85.20927,27.66401],[85.20702,27.66543],[85.20508,27.66716],[85.20153,27.67002],[85.19733,27.67317],[85.19443,27.67546],[85.19249,27.67688],[85.18957,27.67918],[85.19151,27.68262],[85.19279,27.68579],[85.19473,27.68923],[85.19698,27.69326],[85.19891,27.697],[85.20148,27.70016],[85.20535,27.70419],[85.2076,27.70649],[85.21017,27.70908],[85.21501,27.71196],[85.21888,27.71369],[85.22275,27.71514],[85.22694,27.71744],[85.2308,27.72004],[85.23338,27.72291],[85.23434,27.72636],[85.23466,27.72952],[85.23498,27.73354],[85.23562,27.73813],[85.2369,27.7413],[85.24142,27.74446],[85.24496,27.74447],[85.24916,27.74448],[85.25464,27.74765],[85.25819,27.74765],[85.26335,27.74652],[85.26787,27.74796],[85.27013,27.75141],[85.27109,27.75514],[85.27076,27.75916],[85.26979,27.76232],[85.26849,27.76519],[85.26785,27.76777],[85.2672,27.77065],[85.26656,27.77351],[85.26558,27.7761],[85.26106,27.77924]]]
}, 0.01);
console.log(isMended);
Output >
{
"type": "Polygon",
"coordinates": [[[85.26106,27.77924],[85.30396,27.81148],[85.30816,27.81292],[85.45021,27.81423],[85.4515,27.81681],[85.46924,27.78754],[85.47182,27.78409],[85.49957,27.77521],[85.50085,27.77406],[85.49985,27.74448],[85.50049,27.74046],[85.52306,27.72898],[85.52435,27.72812],[85.41853,27.72089],[85.41724,27.7209],[85.35822,27.66887],[85.35435,27.66742],[85.30566,27.69032],[85.30404,27.68974],[85.28441,27.64435],[85.28377,27.64004],[85.2941,27.60618],[85.29411,27.60531],[85.26996,27.57513],[85.26771,27.57282],[85.23258,27.58597],[85.23064,27.58854],[85.23931,27.61526],[85.2377,27.61928],[85.20772,27.62783],[85.20642,27.63012],[85.22734,27.65343],[85.22701,27.65658],[85.19249,27.67688],[85.18957,27.67918],[85.23562,27.73813],[85.2369,27.7413],[85.26335,27.74652],[85.26787,27.74796],[85.26106,27.77924]]]
}
CC0