Skip to content

Commit

Permalink
Closes #1
Browse files Browse the repository at this point in the history
- Rework SPARQL query to fetch data from LINDAS. (Object URIs were
  renamed which caused errors downstream)
- Shorten SPARQL code (makes query faster).
- Remove duplicate entries with R script.
  • Loading branch information
Damian-Oswald committed Aug 5, 2024
1 parent b5f00e3 commit 4f0ac43
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 384 deletions.
3 changes: 3 additions & 0 deletions main.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ source("resources/sparql.R")
# define the SPARQL query (to run this on a federal computer, set proxy server's address)
data = sparql("resources/query.rq", proxy = NULL)

# remove duplicates
data = unique(data)

# subset the data after 2010
data = subset(data, subset = year >= 2010)

Expand Down
16 changes: 3 additions & 13 deletions resources/query-consumer-prices.rq
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
PREFIX cube: <https://cube.link/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX schema: <http://schema.org/>
PREFIX time: <http://www.w3.org/2006/time#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?measure ?year ?month
WHERE {
GRAPH <https://lindas.admin.ch/foag/agricultural-market-data> {
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/138> ) )
FILTER (?valueChainDetail in ( <https://agriculture.ld.admin.ch/foag/value-chain-detail/18> ) )
FILTER (?costComponent in ( <https://agriculture.ld.admin.ch/foag/cost-component/1> ) )
FILTER (?productOrigin in ( <https://agriculture.ld.admin.ch/foag/product-origin/0>, <https://agriculture.ld.admin.ch/foag/product-origin/1> ) )
FILTER (?productionSystem in ( <https://agriculture.ld.admin.ch/foag/production-system/3> ) )
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/239> ) ) # Select UHT milk
FILTER (?productionSystem in ( <https://agriculture.ld.admin.ch/foag/production-system/3> ) ) # Only select conventional milk
<https://agriculture.ld.admin.ch/foag/cube/MilkDairyProducts/Consumption_Price_Month> cube:observationSet ?observationSet .
?observationSet cube:observation ?observation .
?observation <https://agriculture.ld.admin.ch/foag/dimension/cost-component> ?costComponent .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product> ?product .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product-origin> ?productOrigin .
?observation <https://agriculture.ld.admin.ch/foag/dimension/production-system> ?productionSystem .
?observation <https://agriculture.ld.admin.ch/foag/dimension/value-chain-detail> ?valueChainDetail .
?observation <https://agriculture.ld.admin.ch/foag/measure/price> ?measure .
?observation <https://agriculture.ld.admin.ch/foag/dimension/date> ?date .
}
?date time:year ?year.
?date time:month ?month.
} ORDER BY ?year ?month

}
10 changes: 2 additions & 8 deletions resources/query-producer-prices.rq
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
PREFIX cube: <https://cube.link/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX schema: <http://schema.org/>
PREFIX time: <http://www.w3.org/2006/time#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dimension: <https://agriculture.ld.admin.ch/foag/dimension>

SELECT ?month ?year ?measure
WHERE {
GRAPH <https://lindas.admin.ch/foag/agricultural-market-data> {
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/142> ) )
FILTER (?costComponent in ( <https://agriculture.ld.admin.ch/foag/cost-component/0>,<https://agriculture.ld.admin.ch/foag/cost-component/1> ) )
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/243> ) )
FILTER (?productOrigin in ( <https://agriculture.ld.admin.ch/foag/product-origin/1> ) )
<https://agriculture.ld.admin.ch/foag/cube/MilkDairyProducts/Production_Price_Month> cube:observationSet ?observationSet .
?observationSet cube:observation ?observation .
?observation <https://agriculture.ld.admin.ch/foag/dimension/cost-component> ?costComponent .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product> ?product .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product-origin> ?productOrigin .
?observation <https://agriculture.ld.admin.ch/foag/measure/price> ?measure .
?observation <https://agriculture.ld.admin.ch/foag/dimension/date> ?date .
}
?date time:year ?year.
?date time:month ?month.
} ORDER BY ?year ?month
}
23 changes: 6 additions & 17 deletions resources/query.rq
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
PREFIX cube: <https://cube.link/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX schema: <http://schema.org/>
PREFIX time: <http://www.w3.org/2006/time#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?year ?month ?producerPrice ?consumerPrice
WHERE {
{
SELECT ?year ?month ?producerPrice
SELECT ?month ?year ?producerPrice
WHERE {
GRAPH <https://lindas.admin.ch/foag/agricultural-market-data> {
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/142> ) )
FILTER (?costComponent in ( <https://agriculture.ld.admin.ch/foag/cost-component/0>,<https://agriculture.ld.admin.ch/foag/cost-component/1> ) )
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/243> ) )
FILTER (?productOrigin in ( <https://agriculture.ld.admin.ch/foag/product-origin/1> ) )
<https://agriculture.ld.admin.ch/foag/cube/MilkDairyProducts/Production_Price_Month> cube:observationSet ?observationSet .
?observationSet cube:observation ?observation .
?observation <https://agriculture.ld.admin.ch/foag/dimension/cost-component> ?costComponent .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product> ?product .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product-origin> ?productOrigin .
?observation <https://agriculture.ld.admin.ch/foag/measure/price> ?producerPrice .
Expand All @@ -26,26 +21,20 @@ WHERE {
}
}
OPTIONAL {
SELECT ?year ?month ?consumerPrice
SELECT ?consumerPrice ?year ?month
WHERE {
GRAPH <https://lindas.admin.ch/foag/agricultural-market-data> {
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/138> ) )
FILTER (?valueChainDetail in ( <https://agriculture.ld.admin.ch/foag/value-chain-detail/18> ) )
FILTER (?costComponent in ( <https://agriculture.ld.admin.ch/foag/cost-component/1> ) )
FILTER (?productOrigin in ( <https://agriculture.ld.admin.ch/foag/product-origin/0>, <https://agriculture.ld.admin.ch/foag/product-origin/1> ) )
FILTER (?productionSystem in ( <https://agriculture.ld.admin.ch/foag/production-system/3> ) )
FILTER (?product in ( <https://agriculture.ld.admin.ch/foag/product/239> ) ) # Select UHT milk
FILTER (?productionSystem in ( <https://agriculture.ld.admin.ch/foag/production-system/3> ) ) # Only select conventional milk
<https://agriculture.ld.admin.ch/foag/cube/MilkDairyProducts/Consumption_Price_Month> cube:observationSet ?observationSet .
?observationSet cube:observation ?observation .
?observation <https://agriculture.ld.admin.ch/foag/dimension/cost-component> ?costComponent .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product> ?product .
?observation <https://agriculture.ld.admin.ch/foag/dimension/product-origin> ?productOrigin .
?observation <https://agriculture.ld.admin.ch/foag/dimension/production-system> ?productionSystem .
?observation <https://agriculture.ld.admin.ch/foag/dimension/value-chain-detail> ?valueChainDetail .
?observation <https://agriculture.ld.admin.ch/foag/measure/price> ?consumerPrice .
?observation <https://agriculture.ld.admin.ch/foag/dimension/date> ?date .
}
?date time:year ?year.
?date time:month ?month.
}
}
} ORDER BY ?year ?month
}
Loading

0 comments on commit 4f0ac43

Please sign in to comment.