User:Tagishsimon/junk

This page sets out a number of design patterns for SPARQL - that is, code applicable to common problems within query design

Code fragments edit

Exceptions to negation edit

Exclude items based on their membership of a class, but make exceptions for subsets of the class.

Example: There doesn't seem to be a class by which all "sea" items (e.g. sea, ocean, bay, firth, inlet &c &c) can be identified. A "sea" item might be identified as something that a lake or river (in fact a watercourse) flows into, and which is itself not a lake nor a watercourse. Except that estuaries or channels are watercourses, but count as part of the sea.

If we wish to find all UK rivers which flow into the sea, we can look for items with ?country=UK and which have a outflows (P201) or mouth of the watercourse (P403) value, where that value is not not a watercourse, unless the type of watercourse is estuary or channel.

The design pattern excludes watercourses using FILTER NOT EXISTS, nesting two further FILTER NOT EXIST clauses within the watercourse exclusion filter, which serve to exclude estuaries and channels from the watercourse exclusion filter.

#defaultView:Map
#title:UK rivers that enter the sea
SELECT DISTINCT ?river ?riverLabel ?sea ?seaLabel ?coord WHERE 
{
  ?another_river wdt:P17 wd:Q145.          # ?another_river is in the UK
  ?another_river wdt:P403|wdt:P201 ?sea .  # has a mouth of a watercourse or lake outflow 
                                           # at this point ?sea could still be another watercourse
  
                                                                                             # DESIGN PATTERN
                                                                                             # define a ?sea as a sea by...
  FILTER NOT EXISTS {?sea wdt:P31/wdt:P279* wd:Q355304 . hint:Prior hint:gearing "forward".  # excluding watercourses
                    FILTER NOT EXISTS {?sea wdt:P31 wd:Q47053. }                               # except when they're an estuary
                    FILTER NOT EXISTS {?sea wdt:P31 wd:Q1210950. }                             # or a channel
                                                                                            # by nesting FILTER NOT EXIST clauses 
                                                                                            # within a FILTER NOT EXIST clause
                    }

  FILTER NOT EXISTS {?sea wdt:P31/wdt:P279* wd:Q23397 . hint:Prior hint:gearing "forward". } # Exclude lakes
  
  ?sea ^wdt:P403|^wdt:P201 ?river. # ?sea is at the mouth/outflow of the ?river
  ?river wdt:P17 wd:Q145.          # ?river is in the UK
  ?river wdt:P625 ?coord.          # get ?river coordinates
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
UK rivers that enter the sea



  • Nested optionals - river to sea report
  • Producing labels early
  • Getting properties from predicates

Getting things edit

Qualifiers edit

References edit

Sitelinks edit

Units edit

Normalised values edit

Dealing with date & time edit

Time precision edit

Formatting dates edit

  • ISO 8601 dates - yyyy-mm-dd
  • Calendar month labels

Query-in-query edit

Ordering null values below values edit

ORDER BY (!bound(?value))

number range (query on natural numbers?) edit

How to edit

  • Maps, layers, colour
  • Line strings - sea to river report
  • kml
  • Charts
  • MWAPI free text search
  • MWAPI category search
  • Percentiles