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

Add tool wdcoord to get coordinates #6

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For most, those tools are just [wikidata-sdk](https://www.npmjs.com/package/wiki
- [qclaims](#qclaims)
- [qdata](#qdata)
- [wikiqid](#wikiqid)
- [wdcoord](#wdcoord)
- [wdprops](#wdprops)
- [wdprops reset](#wdprops-reset)
- [wdsparl](#wdsparl)
Expand Down Expand Up @@ -93,6 +94,14 @@ wikiqid https://en.wikipedia.org/wiki/Friedrich_Nietzsche
# => Q9358
```

### wdcoord
A command to the the geographic coordinates (latitude and longitude) of a Wikidata item.

```sh
wdcoord Q2112
# => 52.016666666667 8.5166666666667
```

### wdprops
A command to access the list of all Wikidata properties in a given language (by default the environment local language)

Expand Down
64 changes: 64 additions & 0 deletions bin/wdcoord
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env node

var chalk = require('chalk');

// print error message to STDERR and exit
function error(msg) {
console.error(chalk.red(msg))
process.exit(1)
}

function wdcoord(id, env) {
var breq = require('bluereq')
var url = "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + id + "&props=claims";

breq.get(url)
.then(function(res) {
var entities = res.body.entities
var claims = entities && entities[id] && entities[id].claims
if (claims == null) {
error('item or claims not found!')
}
var location = claims['P625']
if (!location || !location.length) return

// emit all coordinates (ignoring rank!)
location.forEach(function(stm) {
var value = stm.mainsnak.datavalue.value
printCoords(value, env.format)
})

return
})
.catch(function(msg) {
error("HTTP request error"+"\n"+msg)
})
}

function printCoords(coords, format) {
var data
if (format == 'json') {
data = JSON.stringify(coords) + "\n"
} else {
data = coords.latitude + " " + coords.longitude + "\n"
}
process.stdout.write(data)
}

var program = require('commander')

program
.arguments('<id>')
.option('-j, --json', 'print Wikidata JSON values')
.description('get geographic coordinates of a Wikidata item')
.action(function(id, env) {
if (!/^Q[0-9]+$/.test(id)) {
error('invalid wikidata Q id!')
}
var format = 'text'
if (env.json) format = 'json'
wdcoord(id, { format: format })
})
.parse(process.argv)

if (!program.args.length) program.help()
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"qlabel": "./bin/qlabel",
"qclaims": "./bin/qclaims",
"wikiqid": "./bin/wikiqid",
"wdcoord": "./bin/wdcoord",
"wdprops": "./bin/wdprops",
"wdsparql": "./bin/wdsparql",
"wdsparqlsimplify": "./bin/wdsparqlsimplify"
Expand All @@ -32,7 +33,9 @@
"dependencies": {
"bluebird": "^3.4.1",
"bluereq": "^2.1.1",
"chalk": "^1.1.3",
"colors": "^1.1.2",
"commander": "^2.9.0",
"copy2cb": "^1.0.1",
"lodash": "^4.12.0",
"request": "^2.72.0",
Expand Down