query-adfd868dded94e1d0d87448018aebba2

rq turtle/ttl

Dataset of localities in Ukraine, and "main administratives", suitable for import in some GIS software.

You will get localities, and their administratives, where the administratives are sorted by a priority factor.

In your import, you may want to skip any duplicate locality, so to import just the first one in order of appearance,

so to import just each locality and its "main administrative" and skip other "minor" administratives.

Author: [[User:Valerio Bozzolan]], Landscapefor.eu Foundation, contributors

Date: 2023-11-10

License: CC0

NOTES:

Please read the footer about these elements:

https://www.wikidata.org/wiki/Q756294 - Republic of Crimea

https://www.wikidata.org/wiki/Q15966495 - Autonomous Republic of Crimea

SELECT ?comune ?comuneLabel (SAMPLE(?location) AS ?location) ?administrative ?administrativeLabel ?index WHERE {

# Force to Ukraine
?comune wdt:P17 wd:Q212.

wd:Q1048835 ^wdt:P279*/^wdt:P31 ?comune.

?comune wdt:P131 ?administrative.

# Exclude former cities. MINUS { wd:Q22674925 ^wdt:P279*/^wdt:P31 ?comune. }

# Exclude countries. MINUS { ?comune wdt:P31 wd:Q6256. }

# Exclude electoral districts. MINUS { ?comune wdt:P31 wd:Q63084210. }

# Exclude neighborhood MINUS { wd:Q123705 ^wdt:P279*/^wdt:P31 ?comune. }

# Exclude administratives that are abolished. MINUS { ?administrative wdt:P576 []. }

# Exclude administratives with end time MINUS { ?comune p:P131 [ pq:P582 [] ]. }

?comune wdt:P625 ?location.

# Raise priority to administrative that is a "Raion" ("Region"). BIND (EXISTS{?administrative wdt:P31 wd:Q1267632} AS ?isUkrainianRaionRaw) BIND (IF(?isUkrainianRaionRaw, 1, 0) AS ?isUkrainianRaion)

# We have to give priority to Q756294 "Republic of Crimea" or to Q15966495 "Autonomous Republic of Crimea" # so to be consistent and pick one of these and not the other one. # It's really not important what we pick, but that should be consistent so to simplify future changes # in your local dataset. # So, I have thrown a dice to decide this and I've picked "Autonomous Republic of Crimea". # I hope this choice will not cause any political nightmare against me, that I'm not opinionated in this # and I'm just trying to import some localities in an Italian geographical atlas. # Feel free to invert if you are opinionated but please share any explaination so we can understand. ThanksĀ :) BIND(IF(STRENDS(STR(?administrative), 'Q15966495'), 1, 0) AS ?isTheChoosenRepublicCrimea)

# Create the final priority factor, so we give priority to meaningful administratives. # So we can import the first row (locality + most important administrative), # and we can skip others lines - (locality + less important administrative). BIND((?isUkrainianRaion + ?isTheChoosenRepublicCrimea) AS ?index)

SERVICE wikibase:label { bd:serviceParam wikibase:language "en,uk,ru" . } }

We want localities, their administrative, and just a sample locality's location.

So, "?location" is aggregated by SAMPLE().

GROUP BY ?comune ?comuneLabel ?administrative ?administrativeLabel ?index

We want localities, ordered by most appropriate administrative.

ORDER BY ASC(?comuneLabel) DESC(?index) ASC(?administrativeLabel)

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# Dataset of localities in Ukraine, and "main administratives", suitable for import in some GIS software.
# You will get localities, and their administratives, where the administratives are sorted by a priority factor.
# In your import, you may want to skip any duplicate locality, so to import just the first one in order of appearance,
# so to import just each locality and its "main administrative" and skip other "minor" administratives.
# Author: [[User:Valerio Bozzolan]], Landscapefor.eu Foundation, contributors
# Date: 2023-11-10
# License: CC0
#
# NOTES:
# Please read the footer about these elements:
## https://www.wikidata.org/wiki/Q756294   - Republic of Crimea
## https://www.wikidata.org/wiki/Q15966495 - Autonomous Republic of Crimea
SELECT
?comune
?comuneLabel
(SAMPLE(?location) AS ?location)
?administrative
?administrativeLabel
?index
WHERE {

  # Force to Ukraine     
  ?comune wdt:P17 wd:Q212.

  wd:Q1048835 ^wdt:P279*/^wdt:P31 ?comune.

  ?comune wdt:P131 ?administrative.

  # Exclude former cities.
  MINUS {
    wd:Q22674925 ^wdt:P279*/^wdt:P31 ?comune.
  }

  # Exclude countries.
  MINUS {
    ?comune wdt:P31 wd:Q6256.
  }

  # Exclude electoral districts.
  MINUS {
    ?comune wdt:P31 wd:Q63084210. 
  }

  # Exclude neighborhood
  MINUS {
      wd:Q123705 ^wdt:P279*/^wdt:P31 ?comune.
  }

  # Exclude administratives that are abolished.
  MINUS {
    ?administrative wdt:P576 []. 
  }

  # Exclude administratives with end time
  MINUS {
    ?comune p:P131 [ pq:P582 [] ].
  }

  ?comune wdt:P625 ?location.

  # Raise priority to administrative that is a "Raion" ("Region").
  BIND (EXISTS{?administrative wdt:P31 wd:Q1267632} AS ?isUkrainianRaionRaw)
  BIND (IF(?isUkrainianRaionRaw, 1, 0) AS ?isUkrainianRaion)

  # We have to give priority to Q756294 "Republic of Crimea" or to Q15966495 "Autonomous Republic of Crimea"
  # so to be consistent and pick one of these and not the other one.
  # It's really not important what we pick, but that should be consistent so to simplify future changes
  # in your local dataset.
  # So, I have thrown a dice to decide this and I've picked "Autonomous Republic of Crimea".
  # I hope this choice will not cause any political nightmare against me, that I'm not opinionated in this
  # and I'm just trying to import some localities in an Italian geographical atlas.
  # Feel free to invert if you are opinionated but please share any explaination so we can understand. ThanksĀ :)
  BIND(IF(STRENDS(STR(?administrative), 'Q15966495'), 1, 0) AS ?isTheChoosenRepublicCrimea)

  # Create the final priority factor, so we give priority to meaningful administratives.
  # So we can import the first row (locality + most important administrative),
  # and we can skip others lines - (locality + less important administrative).
  BIND((?isUkrainianRaion + ?isTheChoosenRepublicCrimea) AS ?index)

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en,uk,ru" .
  }
}

# We want localities, their administrative, and just a sample locality's location.
# So, "?location" is aggregated by SAMPLE().
GROUP BY ?comune ?comuneLabel ?administrative ?administrativeLabel ?index

# We want localities, ordered by most appropriate administrative.
ORDER BY ASC(?comuneLabel) DESC(?index) ASC(?administrativeLabel)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v5("?administrative"):::projected v3("?administrativeLabel"):::projected v4("?comune"):::projected v1("?comuneLabel"):::projected v10("?index"):::projected v9("?isTheChoosenRepublicCrimea") v8("?isUkrainianRaion") v7("?isUkrainianRaionRaw") v10("?location"):::projected a4((" ")) a6((" ")) a1((" ")) a2((" ")) a3((" ")) a5((" ")) c7(["wd:Q22674925"]):::iri c4(["wd:Q1048835"]):::iri c18(["en,uk,ru"]):::literal c2(["wd:Q212"]):::iri c16(["bd:serviceParam"]):::iri c8(["wd:Q6256"]):::iri c9(["wd:Q63084210"]):::iri c10(["wd:Q123705"]):::iri v4 --"p:direct/P17"--> c2 a1 --"p:direct/P279"--> c4 v4 --"p:direct/P31"--> a1 v4 --"p:direct/P131"--> v5 subgraph minus0["MINUS"] style minus0 stroke-width:6px,fill:pink,stroke:red; a2 --"p:direct/P279"--> c7 v4 --"p:direct/P31"--> a2 end subgraph minus1["MINUS"] style minus1 stroke-width:6px,fill:pink,stroke:red; v4 --"p:direct/P31"--> c8 end subgraph minus2["MINUS"] style minus2 stroke-width:6px,fill:pink,stroke:red; v4 --"p:direct/P31"--> c9 end subgraph minus3["MINUS"] style minus3 stroke-width:6px,fill:pink,stroke:red; a3 --"p:direct/P279"--> c10 v4 --"p:direct/P31"--> a3 end subgraph minus4["MINUS"] style minus4 stroke-width:6px,fill:pink,stroke:red; v5 --"p:direct/P576"--> a4 end subgraph minus5["MINUS"] style minus5 stroke-width:6px,fill:pink,stroke:red; a5 --"p:qualifier/P582"--> a6 v4 --"p:P131"--> a5 end v4 --"p:direct/P625"--> v10 v5 --"p:direct/P31"--> null bind6[/" "/] subgraph bind6e0["Exists Clause"] e0v1 --"p:direct/P31"--> e0c2 e0v1("?administrative"):::projected e0c2(["wd:Q1267632"]):::iri end bind6--EXISTS--> bind6e0 v5 --o bind6 c5 --o bind6 null --o bind6 bind6 --as--o v7 bind7[/"if(?isUkrainianRaionRaw,'1^^xsd:integer','0^^xsd:integer')"/] v7 --o bind7 bind7 --as--o v8 bind8[/"if(ends-with(str(?administrative),'Q15966495'),'1^^xsd:integer','0^^xsd:integer')"/] v5 --o bind8 bind8 --as--o v9 bind9[/"?isUkrainianRaion + ?isTheChoosenRepublicCrimea"/] v8 --o bind9 v9 --o bind9 bind9 --as--o v10 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c16 --"wikibase:language"--> c18 end bind11[/"sample(?location)"/] v10 --o bind11 bind11 --as--o v10