1 /*
  2  * AreaUnit.js - Unit conversions for area
  3  *
  4  * Copyright © 2014-2015, 2018 JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 /*
 21 !depends
 22 Measurement.js
 23 */
 24 
 25 var Measurement = require("./Measurement.js");
 26 
 27 /**
 28  * @class
 29  * Create a new area measurement instance.
 30  * @constructor
 31  * @extends Measurement
 32  * @param options {{unit:string,amount:number|string|undefined}} Options controlling
 33  * the construction of this instance
 34  */
 35 var AreaUnit = function (options) {
 36     this.unit = "square-meter";
 37     this.amount = 0;
 38 
 39     this.ratios = AreaUnit.ratios;
 40     this.aliases = AreaUnit.aliases;
 41     this.aliasesLower = AreaUnit.aliasesLower;
 42     this.systems = AreaUnit.systems;
 43 
 44     this.parent.call(this, options);
 45 };
 46 
 47 AreaUnit.prototype = new Measurement();
 48 AreaUnit.prototype.parent = Measurement;
 49 AreaUnit.prototype.constructor = AreaUnit;
 50 
 51 AreaUnit.ratios = {
 52     /*               index        square cm,        square meter,   hectare,       square km,     , square inch     square foot,         square yard,                 acre,                square mile                    */
 53     "square-centimeter":[1,       1,                0.0001,            1e-8,        1e-10,        0.15500031,    0.00107639104,        0.000119599005,            2.47105381e-8,        3.86102159e-11         ],
 54     "square-meter":     [2,       10000,            1,              1e-4,       1e-6,         1550,             10.7639,              1.19599,                   0.000247105,        3.861e-7             ],
 55     "hectare":          [3,         100000000,      10000,          1,          0.01,         1.55e+7,           107639,              11959.9,                   2.47105    ,            0.00386102            ],
 56     "square-kilometer": [4,          10000000000,     1e+6,              100,        1,              1.55e+9,           1.076e+7,            1.196e+6,                  247.105 ,           0.386102             ],
 57     "square-inch":      [5,          6.4516,            0.00064516,     6.4516e-8,  6.4516e-10,   1,            0.0069444444444444, 0.0007716051,             1.5942e-7,            2.491e-10            ],
 58     "square-foot":      [6,        929.0304,        0.092903,       9.2903e-6,  9.2903e-8,    144,            1,                    0.111111,                  2.2957e-5,            3.587e-8            ],
 59     "square-yard":      [7,        8361.2736,        0.836127,       8.3613e-5,  8.3613e-7,    1296,              9,                    1,                         0.000206612,        3.2283e-7            ],
 60     "acre":             [8,        40468564.2,        4046.86,        0.404686,   0.00404686,   6.273e+6,          43560,                4840,                      1,                    0.0015625            ],
 61     "square-mile":      [9,           2.58998811e+10,    2.59e+6,        258.999,    2.58999,      4.014e+9,         2.788e+7,             3.098e+6,                  640,                 1                    ]
 62 }
 63 
 64 /**
 65  * Return the type of this measurement. Examples are "mass",
 66  * "length", "speed", etc. Measurements can only be converted
 67  * to measurements of the same type.<p>
 68  *
 69  * The type of the units is determined automatically from the
 70  * units. For example, the unit "grams" is type "mass". Use the
 71  * static call {@link Measurement.getAvailableUnits}
 72  * to find out what units this version of ilib supports.
 73  *
 74  * @return {string} the name of the type of this measurement
 75  */
 76 AreaUnit.prototype.getMeasure = function() {
 77     return "area";
 78 };
 79 
 80 /**
 81  * Return a new instance of this type of measurement.
 82  *
 83  * @param {Object} params parameters to the constructor
 84  * @return {Measurement} a measurement subclass instance
 85  */
 86 AreaUnit.prototype.newUnit = function(params) {
 87     return new AreaUnit(params);
 88 };
 89 
 90 AreaUnit.aliases = {
 91     "square centimeter":"square-centimeter",
 92     "square centimeters":"square-centimeter",
 93     "square centimetre":"square-centimeter",
 94     "square centimetres":"square-centimeter",
 95     "sq centimeter":"square-centimeter",
 96     "sq centimeters":"square-centimeter",
 97     "sq centimetre":"square-centimeter",
 98     "sq centimetres":"square-centimeter",
 99     "square cm":"square-centimeter",
100     "sq cm":"square-centimeter",
101     "cm2":"square-centimeter",
102     "cm²":"square-centimeter",
103     "square kilometer":"square-kilometer",
104     "square kilometre":"square-kilometer",
105     "square kilometers":"square-kilometer",
106     "square kilometres":"square-kilometer",
107     "sq kilometer":"square-kilometer",
108     "sq kilometre":"square-kilometer",
109     "sq kilometers":"square-kilometer",
110     "sq kilometres":"square-kilometer",
111     "square km":"square-kilometer",
112     "sq km":"square-kilometer",
113     "km2":"square-kilometer",
114     "km²":"square-kilometer",
115     "hectare":"hectare",
116     "ha":"hectare",
117     "square meter": "square-meter",
118     "square meters":"square-meter",
119     "square metre": "square-meter",
120     "square metres": "square-meter",
121     "sq meter": "square-meter",
122     "sq meters":"square-meter",
123     "sq metre": "square-meter",
124     "sq metres": "square-meter",
125     "sqm":"square-meter",
126     "m2": "square-meter",
127     "m²":"square-meter",
128     "square mile":"square-mile",
129     "square miles":"square-mile",
130     "square mi":"square-mile",
131     "sq mi":"square-mile",
132     "mi2":"square-mile",
133     "mi²":"square-mile",
134     "acre": "acre",
135     "acres":"acre",
136     "square yard": "square-yard",
137     "square yards":"square-yard",
138     "sq yard": "square-yard",
139     "sq yards": "square-yard",
140     "sq yrd": "square-yard",
141     "sq yrds": "square-yard",
142     "yard2":"square-yard",
143     "yard²":"square-yard",
144     "yrd2":"square-yard",
145     "yrd²":"square-yard",
146     "yd2":"square-yard",
147     "yd²":"square-yard",
148     "square foot": "square-foot",
149     "square feet": "square-foot",
150     "sq ft":"square-foot",
151     "ft2":"square-foot",
152     "ft²":"square-foot",
153     "square inch":"square-inch",
154     "square inches":"square-inch",
155     "in2":"square-inch",
156     "in²":"square-inch"
157 };
158 
159 (function() {
160     AreaUnit.aliasesLower = {};
161     for (var a in AreaUnit.aliases) {
162         AreaUnit.aliasesLower[a.toLowerCase()] = AreaUnit.aliases[a];
163     }
164 })();
165 
166 /**
167  * Convert a Area to another measure.
168  * @static
169  * @param to {string} unit to convert to
170  * @param from {string} unit to convert from
171  * @param area {number} amount to be convert
172  * @returns {number|undefined} the converted amount
173  */
174 AreaUnit.convert = function(to, from, area) {
175     from = Measurement.getUnitIdCaseInsensitive(AreaUnit, from) || from;
176     to = Measurement.getUnitIdCaseInsensitive(AreaUnit, to) || to;
177     var fromRow = AreaUnit.ratios[from];
178     var toRow = AreaUnit.ratios[to];
179     if (typeof(from) === 'undefined' || typeof(to) === 'undefined') {
180         return undefined;
181     }
182     return area* fromRow[toRow[0]];
183 };
184 
185 /**
186  * @private
187  * @static
188  */
189 AreaUnit.getMeasures = function () {
190     return Object.keys(AreaUnit.ratios);
191 };
192 
193 AreaUnit.systems = {
194     "metric": [
195         "square-centimeter",
196         "square-meter",
197         "hectare",
198         "square-kilometer"
199     ],
200     "imperial": [
201         "square-inch",
202         "square-foot",
203         "square-yard",
204         "acre",
205         "square-mile"
206     ],
207     "uscustomary": [
208         "square-inch",
209         "square-foot",
210         "square-yard",
211         "acre",
212         "square-mile"
213     ],
214     "conversions": {
215         "metric": {
216             "uscustomary": {
217                 "square-centimeter" : "square-inch",
218                 "square-meter" : "square-yard",
219                 "hectare" : "acre",
220                 "square-kilometer" : "square-mile"
221             },
222             "imperial": {
223                 "square-centimeter" : "square-inch",
224                 "square-meter" : "square-yard",
225                 "hectare" : "acre",
226                 "square-kilometer" : "square-mile"
227             }
228         },
229         "uscustomary": {
230             "metric": {
231                 "square-inch" : "square-centimeter",
232                 "square-foot" : "square-meter",
233                 "square-yard" : "square-meter",
234                 "acre" : "hectare",
235                 "square-mile" : "square-kilometer"
236             }
237         },
238         "imperial": {
239             "metric": {
240                 "square-inch" : "square-centimeter",
241                 "square-foot" : "square-meter",
242                 "square-yard" : "square-meter",
243                 "acre" : "hectare",
244                 "square-mile" : "square-kilometer"
245             }
246         }
247     }
248 };
249 
250 //register with the factory method
251 Measurement._constructors["area"] = AreaUnit;
252 
253 module.exports = AreaUnit;
254