You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
No one has agreed on a standard way of representing lon/lat. This is a small normalization library. Use this to convert all outside input before processing internally and convert to an external format right when it's being output.
9
9
10
-
## Just use the `{lon: ${longitude}, lat: ${latitude}}` representation
10
+
## API
11
11
12
-
Utilizing this won't always be possible/easiest, so please at least adopt the following conventions. Any variables or functions that contain the following names should be represented by the accompanying structure:
var position =lonlat.fromPoint({ x:12, y:34 }) // { lon: 12, lat: 34 }
56
115
```
57
116
117
+
### lonlat.fromString(str)
118
+
119
+
Tries to parse from a string. Will throw an error upon finding invalid coordinates.
120
+
121
+
#### Arguments
122
+
123
+
`str (string)`: A string in the format: '{longitude},{latitude}'
124
+
125
+
#### Returns
126
+
127
+
`(Object)`: An object with `lon` and `lat` attributes.
128
+
129
+
#### Example
130
+
131
+
```js
132
+
var lonlat =require('@conveyal/lonlat')
133
+
134
+
var position =lonlat.fromString('12,34') // { lon: 12, lat: 34 }
135
+
```
136
+
137
+
### lonlat.print(input, [fixed=5])
138
+
139
+
Returns a pretty string
140
+
141
+
#### Arguments
142
+
143
+
-`input (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
144
+
-`[fixed=5] (Number)`: The number of digits to round to
145
+
146
+
#### Returns
147
+
148
+
`(string)`: A string with the latitude and longitude rounded to the number of decimal places as specified by `fixed`
149
+
150
+
#### Example
151
+
152
+
```js
153
+
var lonlat =require('@conveyal/lonlat')
154
+
155
+
var pretty =lonlat.print('12.345678,34') // '12.34568, 34.00000'
156
+
```
157
+
158
+
### lonlat.isEqual(lonlat1, lonlat2, [epsilon=0])
159
+
160
+
Checks equality of two inputs within an allowable difference.
161
+
162
+
#### Arguments
163
+
164
+
-`lonlat1 (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
165
+
-`lonlat2 (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
166
+
-`[epsilon=0] (Number)`: The maximum allowable difference of between each latitude and longitude
167
+
168
+
#### Returns
169
+
170
+
`(boolean)`: Returns `true` if the inputs are equal or `false` if they are not
171
+
172
+
#### Example
173
+
174
+
```js
175
+
var lonlat =require('@conveyal/lonlat')
176
+
177
+
var isEqual =lonlat.isEqual('12,34', [12, 34]) // true
178
+
```
179
+
180
+
### lonlat.toCoordinates(input)
181
+
182
+
Translates to a coordinate array.
183
+
184
+
#### Arguments
185
+
186
+
`input (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
187
+
188
+
#### Returns
189
+
190
+
`(Array)`: An array in the format: [longitude, latitude]
191
+
192
+
#### Example
193
+
194
+
```js
195
+
var lonlat =require('@conveyal/lonlat')
196
+
197
+
var coords =lonlat.toCoordinates('12,34') // [12, 34]
198
+
```
199
+
200
+
### lonlat.toPoint(input)
201
+
202
+
Translates to point Object.
203
+
204
+
#### Arguments
205
+
206
+
`input (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
207
+
208
+
#### Returns
209
+
210
+
`(Object)`: An object with `x` and `y` attributes representing latitude and longitude respectively
211
+
212
+
#### Example
213
+
214
+
```js
215
+
var lonlat =require('@conveyal/lonlat')
216
+
217
+
var point =lonlat.toPoint('12,34') // { x: 12, y: 34 }
218
+
```
219
+
220
+
### lonlat.toString(input)
221
+
222
+
Translates to coordinate string.
223
+
224
+
#### Arguments
225
+
226
+
`input (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
227
+
228
+
#### Returns
229
+
230
+
`(string)`: A string in the format 'latitude,longitude'
231
+
232
+
#### Example
233
+
234
+
```js
235
+
var lonlat =require('@conveyal/lonlat')
236
+
237
+
var str =lonlat.toString({ lat:12, long:34 }) // '12,34'
238
+
```
239
+
240
+
### lonlat.toLeaflet(input)
241
+
242
+
Translates to [Leaflet LatLng](http://leafletjs.com/reference.html#latlng) object. This function requires Leaflet to be installed as a global variable `L` in the window environment.
243
+
244
+
#### Arguments
245
+
246
+
`input (*)`: Any format mentioned in [lonlat(input)](#lonlatinput)
247
+
248
+
#### Returns
249
+
250
+
`(Object)`: A [Leaflet LatLng](http://leafletjs.com/reference.html#latlng) object
251
+
252
+
#### Example
253
+
254
+
```js
255
+
var lonlat =require('@conveyal/lonlat')
256
+
257
+
var position =lonlat.toLeaflet({ lat:12, long:34 }) // Leaflet LatLng object
0 commit comments