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

Lisenser og lenker til datasett på Geonorge #504

Merged
merged 4 commits into from
Mar 25, 2019
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ build/
node_modules/
.vscode/
kildedata/raster_index.json
log/
log/
nin-data/
13 changes: 13 additions & 0 deletions lib/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const parse = require("csv-parse/lib/sync")
const fs = require("fs")
var JSONStream = require("JSONStream")

function les(csvFilePath) {
const input = fs.readFileSync(csvFilePath)
const records = parse(input, {
columns: true
})
return records
}

module.exports = { les }
21 changes: 21 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const execSync = require("child_process").execSync
const fs = require("fs")

function git(cmd, args = "") {
execSync("git " + cmd + " " + args)
}

function clone(url, destFolder) {
if (fs.existsSync(destFolder)) pull(destFolder)
// only latest version
else {
fs.mkdirSync(destFolder)
git(`clone --depth=1 ${url}`)
}
}

function pull(destFolder) {
git(`-C ${destFolder} pull`)
}

module.exports = { clone }
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"JSONStream": "^1.3.5",
"babel-eslint": "^10.0.0",
"csv": "^5.1.1",
"csv-parse": "^4.3.4",
"decompress-zip": "^0.2.2",
"eslint-config-airbnb-base": "^13.1.0",
"fs-extra": "^7.0.1",
Expand Down
5 changes: 5 additions & 0 deletions steg/01_nedlasting/inn_nin-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = require("../../config")
const git = require("../../lib/git")

// Download "Natur i Norge" data kildedata
git.clone("https://github.com/Artsdatabanken/nin-data.git", "nin-data")
47 changes: 30 additions & 17 deletions steg/05/organisasjon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@ const io = require("../../lib/io")
const log = require("log-less-fancy")()
const config = require("../../config")
const typesystem = require("@artsdatabanken/typesystem")
const csv = require("../../lib/csv")

const organisasjonTilKode = {}
let organisasjon = io.lesKildedatafil("Datakilde/organisasjon")
let datasett = io.lesKildedatafil("Datakilde/datasett")
Object.entries(organisasjon).forEach(([kode, o]) => {
organisasjonTilKode[o.tittel.nb] = kode
organisasjonTilKode[kode] = kode
organisasjonTilKode[kode.replace("OR-", "")] = kode
})

function lagRelasjonTilDatasett(kilde) {
Object.keys(datasett).forEach(key => {
const o = organisasjon[key]
const sett = datasett[key]
o.relasjon = sett.map(s => {
return {
kode: s,
kant: "Datasett",
kantRetur: "Datakilde",
kantReturFraAlleBarna: true,
erSubset: true
}
let datasett = csv.les("nin-data/Natur_i_Norge/datasett.csv")
// Sorter slik at mer spesifikke koder kommer sist og overstyrer generell datakilde
datasett.sort((a, b) => a.Datasett.length - b.Datasett.length)
lagRelasjonTilDatasett(datasett)
io.skrivDatafil(__filename, organisasjon)

function lagRelasjonTilDatasett(datasett) {
datasett.forEach(ds => {
const { Datasett: kode, Dataleverandør, ...datasett } = ds
const orgkode = organisasjonTilKode[Dataleverandør]
if (!orgkode) throw new Error("Ukjent dataleverandør: ", Dataleverandør)
const o = organisasjon[orgkode]
if (!o.relasjon) o.relasjon = []
const rel = {
kode: kode,
kant: "Datasett",
kantRetur: "Datakilde",
kantReturFraAlleBarna: true,
erSubset: true
}
Object.entries(datasett).forEach(([key, value]) => {
rel[key.toLowerCase()] = value
})
o.relasjon.push(rel)
})
}

lagRelasjonTilDatasett()

io.skrivDatafil(__filename, organisasjon)
66 changes: 51 additions & 15 deletions steg/09/full_med_graf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ const typesystem = require("@artsdatabanken/typesystem")
let full = io.lesDatafil("full")
let hierarki = io.lesDatafil("kodehierarki")
const barnAv = hierarki.barn
const skalPropageresNed = []

Object.keys(full).forEach(kode => lagGrafkoblinger(kode, full[kode]))
Object.keys(full).forEach(kode => lagGradientPåSegSelv(kode, full[kode]))
Object.keys(full).forEach(kode => lagGrafGradientkoblinger(kode, full[kode]))
Object.keys(full).forEach(kode => propagerGradientTilRelasjon(kode, full[kode]))
propagerGrafkoblinger()

io.skrivDatafil(__filename, full)

Expand Down Expand Up @@ -48,34 +51,61 @@ function lagGrafkobling(kodeFra, kodeTil, kant, metadata, erSubset) {
}

if (!nodeFra.graf) nodeFra.graf = {}
if (!nodeFra.graf[kant]) nodeFra.graf[kant] = {}
let kobling = Object.assign({}, metadata, tilBarn(nodeTil))
kobling.type = nodeTil.type
if (kobling.kant === "Datasett" && nodeFra.graf["Datakilde"]) return
if (!nodeFra.graf[kant]) nodeFra.graf[kant] = {}
if (nodeTil.type === "flagg" && kobling.kant !== "Datasett") {
if (!nodeFra.flagg) nodeFra.flagg = {}
nodeFra.flagg[kodeTil] = {
tittel: nodeTil.tittel
}
} else {
kobling.erSubset = erSubset
delete kobling.kode
delete kobling.kant
delete kobling.kantRetur
delete kobling.kantReturFraAlleBarna
nodeFra.graf[kant][kodeTil] = kobling
return true
}

kobling.erSubset = erSubset
delete kobling.kode
delete kobling.kant
delete kobling.kantRetur
delete kobling.kantReturFraAlleBarna
nodeFra.graf[kant][kodeTil] = kobling
return true
}

function lagGrafkoblingerTilAlleBarna(
kodeFra,
kodeTil,
kant,
metadata,
erSubset
) {
const barna = barnAv[kodeFra] || []
lagGrafkobling(kodeFra, kodeTil, kant, metadata, erSubset)
barna.forEach(kodeFraBarn => {
lagGrafkoblingerTilAlleBarna(kodeFraBarn, kodeTil, kant, metadata, erSubset)
if (lagGrafkobling(kodeFra, kodeTil, kant, metadata, erSubset)) {
const barna = barnAv[kodeFra] || []
barna.forEach(kodeFraBarn => {
lagGrafkoblingerTilAlleBarna(
kodeFraBarn,
kodeTil,
kant,
metadata,
erSubset
)
})
}
}

function propagerGrafkoblinger() {
skalPropageresNed.forEach(e => {
if (e.kode === "NN-NA-BS") debugger
const barna = barnAv[e.kode] || []
barna.forEach(barnkode =>
lagGrafkoblingerTilAlleBarna(
barnkode,
e.tilKode,
e.kantRetur,
e,
e.erSubset
)
)
})
}

Expand All @@ -85,9 +115,15 @@ function lagGrafkoblinger(kode, node) {
if (!e.kode) throw new Error("Mangler kode " + e.kode)
lagGrafkobling(kode, e.kode, e.kant, e, e.erSubset)
if (e.kantRetur) {
if (e.kantReturFraAlleBarna) {
lagGrafkoblingerTilAlleBarna(e.kode, kode, e.kantRetur, e, e.erSubset)
} else lagGrafkobling(e.kode, kode, e.kantRetur, e, false)
lagGrafkobling(
e.kode,
kode,
e.kantRetur,
e,
e.kantReturFraAlleBarna && e.erSubset
)
if (e.kantReturFraAlleBarna)
skalPropageresNed.push({ ...e, tilKode: kode })
}
})
delete node.relasjon
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,11 @@ csv-parse@^4.3.0:
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-4.3.1.tgz#b2b92a4068106d6713952050593b2be06ec1a577"
integrity sha512-1V98UTtfefu8yKdYIGX1LFhfE2yMllveq2uCBay5y4ybfTzvW6I4M6r8Yc2YnKJdJBUig5ksEMh/bLqKg4vEMQ==

csv-parse@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-4.3.4.tgz#fc896c170ebbdf6fb286de85c41bbaea4973d25f"
integrity sha512-M1R4WL+vt81+GnkKzi0s1qQM6WXvHQKDecNkpozzAEG8LHvIW9bq5eBnOKFQn50fTuAos7JodBh/07MK+J6G2Q==

csv-stringify@^5.1.2:
version "5.3.0"
resolved "https://registry.yarnpkg.com/csv-stringify/-/csv-stringify-5.3.0.tgz#ff2dfafa6fcccd455ff5039be9c202475aa3bbe0"
Expand Down