Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal coordinate to sexagesimal #246

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,22 @@ geolib.decimalToSexagesimal(51.49611111); // -> 51° 29' 46`

Returns the new value as sexagesimal string.

### `decimalCoordinateToSexagesimal(value)`

Converts decimal coordinate object to sexagesimal format

```js
geolib.decimalCoordinateToSexagesimal({
latitude: 121.135,
longitude: 50,
}); // ->{
// latitude: '121° 08' 06" N',
// longitude: '50° 00' 00" W',
// }
```

Returns the new value as an object with both sexagesimal strings.

### `geolib.getLatitude(point, raw = false)`

### `geolib.getLongitude(point, raw = false)`
Expand Down
51 changes: 51 additions & 0 deletions src/decimalCoordinateToSexagesimal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import decimalCoordinateToSexagesimal from './decimalCoordinateToSexagesimal';

describe('decimalCoordinateToSexagesimal', () => {
it('should return correct value for coordinates >0', () => {
expect(
decimalCoordinateToSexagesimal({
latitude: 121.135,
longitude: 50,
})
).toEqual({
latitude: '121° 08\' 06" N',
longitude: '50° 00\' 00" W',
});
});

it('should return correct value for coordinates <0', () => {
expect(
decimalCoordinateToSexagesimal({
latitude: -121.135,
longitude: -50,
})
).toEqual({
latitude: '121° 08\' 06" S',
longitude: '50° 00\' 00" E',
});
});

it('should return correct value for lat >0 and long <0', () => {
expect(
decimalCoordinateToSexagesimal({
latitude: 121.135,
longitude: -50,
})
).toEqual({
latitude: '121° 08\' 06" N',
longitude: '50° 00\' 00" E',
});
});

it('should return correct value for lat <0 and long >0', () => {
expect(
decimalCoordinateToSexagesimal({
latitude: -121.135,
longitude: 50,
})
).toEqual({
latitude: '121° 08\' 06" S',
longitude: '50° 00\' 00" W',
});
});
});
21 changes: 21 additions & 0 deletions src/decimalCoordinateToSexagesimal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import decimal2sexagesimal from './decimalToSexagesimal';
import getLatitude from './getLatitude';
import getLongitude from './getLongitude';
import { GeolibInputCoordinates } from './types';

// Converts a decimal coordinate object to sexagesimal format
const decimalCoordinateToSexagesimal = (coordinate: GeolibInputCoordinates) => {
const latitude = getLatitude(coordinate);
const longitude = getLongitude(coordinate);

return {
latitude: `${decimal2sexagesimal(latitude)} ${
latitude < 0 ? 'S' : 'N'
}`,
longitude: `${decimal2sexagesimal(longitude)} ${
longitude < 0 ? 'E' : 'W'
}`,
};
};

export default decimalCoordinateToSexagesimal;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { default as convertArea } from './convertArea';
export { default as convertDistance } from './convertDistance';
export { default as convertSpeed } from './convertSpeed';
export { default as decimalToSexagesimal } from './decimalToSexagesimal';
export { default as decimalCoordinateToSexagesimal } from './decimalCoordinateToSexagesimal';
export { default as findNearest } from './findNearest';
export { default as getAreaOfPolygon } from './getAreaOfPolygon';
export { default as getBounds } from './getBounds';
Expand Down