Adrian Gschwend
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # RDF Data Cube Library ## Getting all dimensions, measures & attributes To properly query the data we first need some basic metadata, to be able to generate a query for the observation the easiest way is to fetch all dimensions, attributes and measures for the datacube. * dimensions are mandatory, in terms of every observation needs to have *all* dimensions * measure(s) is the measure itself. It can be one or more. I think in our cubes it's always only one * attributes: this is additional stuff that is related to the observation but can be optional. So it has to be OPTIONAL in SPARQL This query works on https://lindas-data.ch/sparql-ui/ with all the `FROM` statements, use them to switch between different cubes ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT * # comment one of these FROMs to see what happens FROM <https://linked.opendata.swiss/graph/FOEN/UBD28> #FROM <https://linked.opendata.swiss/graph/FOEN/UBD66> # in the two cases above you only get one dataset back. # This one here has a whole bunch of them: #FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { ?dataset a qb:DataSet ; qb:structure/qb:component ?componentSpec . # kind: is it a dimension/attribute or measure # Note that you can basically filter on this URI, look up # dimension/attribute/measure on our service: # https://prefix.zazuko.com/qb:dimension ?componentSpec ?kind ?componentUri . # obviously the label could be multi-language, so we need to be # able to filter for that for the user ?componentUri rdfs:label|skos:prefLabel ?label . } ``` For Zurich we get a whole bunch of datasets back, in their documentation they show how to get a list of all datasets, plus labels, plus the amount of observations per dataset: https://github.com/StatistikStadtZuerich/documentation#list-of-available-datasets If you would like to get the metadata for one particular dataset, just replace `?dataset` with the URI of the particular dataset. For `FOEN` graphs this won't make a difference as there is only one dataset per graph. In Zurich you will get far less dimensions back that way. Example: `<https://ld.stadt-zuerich.ch/statistics/dataset/AST-RAUM-ZEIT-BTA> a qb:DataSet ;` ## Fetching data Each observation is stored as a `qb:Observation`, the most minimal query would simply fetch all observations in a store and we could start working with it. Obviously that's exactly what we do not want to do, as this might give back way too many data that way. So as a next step we need to figure out the proper minimal query for the dataset we want to query. The minimal query would be: ```sparql ?observation a qb:Observation ; <dimension1> ?uriOfDimension1 ; <dimension2> ?uriOfDimension2 ; <dimensionN> ?uriOfDimensionN ; ``` As mentioned before, for a specific DataCube, *all* dimensions need to be specified by an Observation. In the query above we had a list of dimensions in the combination of `?kind` and `?componentUri`. If we would filter (or replace) `?kind` with `qb:dimension`, we would get all necessary `?componentUri`s back which we need to create a minimal query for observations. Example: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT * # comment one of these FROMs to see what happens #FROM <https://linked.opendata.swiss/graph/FOEN/UBD28> #FROM <https://linked.opendata.swiss/graph/FOEN/UBD66> # in the two cases above you only get one dataset back. # This one here has a whole bunch of them: FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> a qb:DataSet ; qb:structure/qb:component ?componentSpec . # kind: is it a dimension/attribute or measure # Note that you can basically filter on this URI, look up dimension/attribute/measure on our service https://prefix.zazuko.com/qb:dimension ?componentSpec qb:dimension ?componentUri . ?componentUri rdfs:label|skos:prefLabel ?label . # obviously the label could be multi-language, so we need to be able to filter for that for the user } LIMIT 10 ``` So we filter on `qb:dimension` and hardcode the dataset to `<https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT>`. We only get two dimensions back here: RAUM & ZEIT. So a minimal query to get Observations of that DataSet back would be: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT * FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { ?observation a qb:Observation ; <https://ld.stadt-zuerich.ch/statistics/property/ZEIT> ?zeit ; <https://ld.stadt-zuerich.ch/statistics/property/RAUM> ?raum ; qb:dataSet <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> . } LIMIT 10 ``` Note that I added another restriction: ?observation qb:dataSet <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> ; This is also mandatory, otherwise we will get any other Observation back from other DataCubes that also use RAUM & ZEIT as dimensions. What we do not get back is the measure itself, so we have no idea what exactly was measured in this RAUM & ZEIT. If you replace `qb:dimension` in the query before with `qb:measure` you will get the URI of the measure for this dataset: `<https://ld.stadt-zuerich.ch/statistics/measure/BEW>` So simply add this to the query for `qb:Observation`s. Last but not least we might want to add all attributes. To get them properly we need to add each one of them as OPTIONAL. First we replace `qb:dimension` with `qb:attribute` to filter them, then we can generate the following, final query for this dataset: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT * FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { ?observation a qb:Observation ; <https://ld.stadt-zuerich.ch/statistics/property/ZEIT> ?zeit ; <https://ld.stadt-zuerich.ch/statistics/property/RAUM> ?raum ; <https://ld.stadt-zuerich.ch/statistics/measure/BEW> ?measure ; qb:dataSet <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> . OPTIONAL { ?observation <https://ld.stadt-zuerich.ch/statistics/attribute/QUELLE> ?attQuelle . } OPTIONAL { ?observation <https://ld.stadt-zuerich.ch/statistics/attribute/GLOSSAR> ?attGlossar . } OPTIONAL { ?observation <https://ld.stadt-zuerich.ch/statistics/attribute/FUSSNOTE> ?attFussnote . } OPTIONAL { ?observation <https://ld.stadt-zuerich.ch/statistics/attribute/DATENSTAND> ?attDatenstand . } OPTIONAL { ?observation <https://ld.stadt-zuerich.ch/statistics/attribute/ERWARTETE_AKTUALISIERUNG> ?attErwarteteAktualisierung . } OPTIONAL { ?observation <https://ld.stadt-zuerich.ch/statistics/attribute/KORREKTUR> ?attKorrektur . } } LIMIT 10 ``` Implementation note: `OPTIONAL` blocks in SPARQL can really kill performance so it might be a smart thing to have them, pun intended, optional in the library. Often they are not of interest for the users and only those providing the data really care about what is written in there. ## Fetching labels for dimensions There are basically two types of dimensions (and to some extent attributes): Those that have a URI as "instance" of that particular dimension and those who have a literal (most probably a typed literal like `xsd:date`) as instance of the dimension. We can filter both in SPARQL so we can figure out if it's a literal or if it's a URI. For literals we most probably will have to be able to filter for it. The most common literal is obviously dates so we need to be able to create this kind of `FILTER`s: FILTER(?time = "2017-12-31"^^xsd:date) This could also be any time range with `<` and `>`. For the start I propose to simply add time based filtering and see where we go from there. Spatial domains in the Zurich dataset are assigned as URI. For example we have the following URI used: `<https://ld.stadt-zuerich.ch/statistics/code/R30000>`. If we open this URI we see that this is `Stadt Zürich (ab 1934)`. In other words if we want to allow the user to filter for URI based dimensions, we need to fetch the labels from them and once the specific one(s) is selected, filter for it based on its URI. A simple filter would look like: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT * FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { ?observation a qb:Observation ; <https://ld.stadt-zuerich.ch/statistics/property/ZEIT> ?zeit ; <https://ld.stadt-zuerich.ch/statistics/property/RAUM> <https://ld.stadt-zuerich.ch/statistics/code/R30000> ; <https://ld.stadt-zuerich.ch/statistics/measure/BEW> ?measure ; qb:dataSet <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> . } LIMIT 10 ``` So basically we hard-code the URI of the object with the one for Zurichs city center itself. This could also be done with a `FILTER`, even though this might be a bit less performant (depending on the query optimizer obviously): FILTER( ?raum IN(<https://ld.stadt-zuerich.ch/statistics/code/R30000>)) Good thing of that notation is that we can specify multiple URIs, separated by `,`. ## Getting instances of a particular dimension In the wild a bunch of predicates are common for labels, as you have seen in ontologies. In our cubes we use either `rdfs:label` or `skos:prefLabel`. These two should be supported for the moment and it should be easy to add new ones when necessary. This query would fetch all referenced instances of dimension RAUM in a particular dataset: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT ?raum ?raumLabel FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { ?observation a qb:Observation ; <https://ld.stadt-zuerich.ch/statistics/property/RAUM> ?raum ; qb:dataSet <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> . ?raum rdfs:label|skos:prefLabel ?raumLabel . } ``` So that could be used to offer filter lists to users. ## Getting min/max values for instances that are a literal To be able to do proper rendering it's nice to know when a time range starts & ends without querying the full dataset. This query would provide this information for all dimension/measures/attributes: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX qb: <http://purl.org/linked-data/cube#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT ?property (MIN(?propertyValue) AS ?min) (MAX(?propertyValue) AS ?max) FROM <https://linked.opendata.swiss/graph/zh/statistics> WHERE { ?obs a qb:Observation ; qb:dataSet <https://ld.stadt-zuerich.ch/statistics/dataset/BEW-RAUM-ZEIT> ; ?property ?propertyValue . FILTER(isLiteral(?propertyValue)) } GROUP BY ?property ``` ## Mapping to JS objects Michael did quite some work regarding mapping SPARQL result sets to JS, please have a look at [d3-sparql](https://github.com/zazuko/d3-sparql) and either re-use or abstract this into a separate library. There is one special case we did probably not take care of yet: There is no `null` value in RDF, expressing that something was measured but for whatever reason the value is not a number, we use `NaN`, originated in XML. In Zurich dataset this might be the case and in RDF it looks like this: https://discuss.rdf.community/t/discovering-rdf-data-cubes/75/3?u=ktk # Next If we can create this kind of queries, we in my opinion have a basic library available on which we can gather more ideas. ## Ideas ### Michael - dimension continuous / not continuous (nominal dimension) - ~~max / min of continuous (e.g. -10 to +200) or all values of not continuous (Altdorf, St. Gallen ...)~~

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully