extracting only postcode and lsoa code - sparkql

2019-03-02 13:22发布

I am trying to write some sparkql to extract postcode data and marching lsoa codes. what I have so far extracts all the url for both the postcodes and lsoa codes, when I would like just the last unit. How do i get just the last elements please?

query <- "PREFIX pc: <http://data.ordnancesurvey.co.uk/ontology/postcode/>
      PREFIX geo: <http://opendatacommunities.org/def/geography#>
      SELECT * WHERE { 
      ?postcodeUnit
      a pc:PostcodeUnit;
      geo:lsoa ?lsoa .
      }
      limit 10"

   endpoint <- "http://opendatacommunities.org/sparql"
   resultList <- SPARQL(endpoint,query)
   head(resultList)

I am also concerned when i get this working, R studio will time out, do i need to run this from a web server? please

Thanks in advance

1条回答
别忘想泡老子
2楼-- · 2019-03-02 13:57

If the way URIs are generated is standard, you can just turn the results into strings and then take only the part that is needed:

PREFIX pc: <http://data.ordnancesurvey.co.uk/ontology/postcode/>
PREFIX geo: <http://opendatacommunities.org/def/geography#>
SELECT ?postcode ?lsoa
WHERE { 
  ?URI_postcodeUnit
  a pc:PostcodeUnit;
  geo:lsoa ?URI_lsoa .
BIND (STRAFTER((STR(?URI_postcodeUnit)),"postcodeunit/") as ?postcode)
BIND (STRAFTER((STR(?URI_lsoa)),"lsoa/") as ?lsoa)
  }
  limit 10
查看更多
登录 后发表回答