SPARQL

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

SPARQL

#1

Postby Steve James » Thu Sep 08, 2016 9:33 am

Hi, has anyone any experience of SPARQL and the potential of consuming queries within Ebase:?:

http://landregistry.data.gov.uk/app/qonsole#

Thanks
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#2

Postby Steve James » Mon Sep 19, 2016 9:13 am

I got a reply direct from the Land Registry. It is actually farily straightforward once you have the select statement (which will be most of the issue). Ebase doesn't encode # as %23 so I did this via a text replace and have reported it via the portal.
To do this you need to access the SPARQL endpoint using a HTTP get request, there are a number of ways you can achieve this,

You can access the data under both unix and windows platforms.
As an example within unix you can use wget to issue a query to the SPARQL end point.

wget http://landregistry.data.gov.uk/landreg ... ry="SELECT DISTINCT * {?s ?a ?c } LIMIT 1"

You need to ensure you are pointing to the land registry sparql endpoint which in this case is everything before the ?query.
i.e. http://landregistry.data.gov.uk/landregistry/query

Further information on the SPARQL protocol and how to access the data in the triple store via the API can be viewed here. It includes details of how to use HTTP GET and how to format the data retrieved.
http://www.w3.org/TR/sparql11-protocol/

Also attached is an example of HTTP get using ruby N.B. The SPARQL query is for illustrative purposes.

Code: Select all

require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'thread'
require 'net/http'
require 'json'
driver = Selenium::WebDriver.for :firefox

class Sparqlresult
	def initialize(json_data)  
		# Instance variables  
		@json_data = json_data  
	end  

  def display  
    @json_data["results"]["bindings"].each do |child|
		puts "text = #{(child["ppd_pricePaid"]["value"])}"
	end
  end  
  
  def avgeDetached
  i = 0
  sum = 0
  average= 0
   @json_data["results"]["bindings"].each do |child|
		if (child["ppd_propertyType"]["value"]) == "http://landregistry.data.gov.uk/def/common/detached"
			sum = sum + (child["ppd_pricePaid"]["value"]).to_i
			puts sum
			i = i + 1
		end
	end
	average = sum / i
	return(average)
  end  
end  

postcode = "DT*"
county1 = "DEVON"
# Define the query we want to run. We can add as many lines as we
# want to between the EOF markers
test_query = <<EOF
prefix rdf&#58; <http>
prefix rdfs&#58; <http>
prefix owl&#58; <http>
prefix xsd&#58; <http>
prefix sr&#58; <http>
prefix lrhpi&#58; <http>
prefix lrppi&#58; <http>
prefix skos&#58; <http>
prefix lrcommon&#58; <http>

PREFIX  text&#58; <http>
PREFIX  ppd&#58;  <http>
PREFIX  lrcommon&#58; <http>
SELECT  ?item ?ppd_propertyAddress ?ppd_hasTransaction ?ppd_pricePaid ?ppd_transactionCategory ?ppd_transactionDate ?ppd_transactionId ?ppd_estateType ?ppd_newBuild ?ppd_propertyAddressCounty ?ppd_propertyAddressDistrict ?ppd_propertyAddressLocality ?ppd_propertyAddressPaon ?ppd_propertyAddressPostcode ?ppd_propertyAddressSaon ?ppd_propertyAddressStreet ?ppd_propertyAddressTown ?ppd_propertyType ?ppd_recordStatus
WHERE
&#123;?ppd_propertyAddress text&#58;query _&#58;b0 .
_&#58;b0 <http> "county&#58; &#40; &#58;county1 &#41;" .
_&#58;b0 <http> _&#58;b1 .
_&#58;b1 <http> 3000000 .
_&#58;b1 <http> <http> .
?item ppd&#58;propertyAddress ?ppd_propertyAddress .
?item ppd&#58;hasTransaction ?ppd_hasTransaction .
?item ppd&#58;pricePaid ?ppd_pricePaid .
?item ppd&#58;transactionCategory ?ppd_transactionCategory .
?item ppd&#58;transactionDate ?ppd_transactionDate .
?item ppd&#58;transactionId ?ppd_transactionId
OPTIONAL
&#123; ?item ppd&#58;estateType ?ppd_estateType &#125;
OPTIONAL
&#123; ?item ppd&#58;newBuild ?ppd_newBuild &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;county ?ppd_propertyAddressCounty &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;district ?ppd_propertyAddressDistrict &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;locality ?ppd_propertyAddressLocality &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;paon ?ppd_propertyAddressPaon &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;postcode ?ppd_propertyAddressPostcode &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;saon ?ppd_propertyAddressSaon &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;street ?ppd_propertyAddressStreet &#125;
OPTIONAL
&#123; ?ppd_propertyAddress lrcommon&#58;town ?ppd_propertyAddressTown &#125;
OPTIONAL
&#123; ?item ppd&#58;propertyType ?ppd_propertyType &#125;
OPTIONAL
&#123; ?item ppd&#58;recordStatus ?ppd_recordStatus &#125;
FILTER &#40; ?ppd_transactionDate >= "2015-01-01"^^xsd&#58;date &#41;
&#125;
LIMIT   100
EOF
# Specify the host server
#sparql_endpoint = "http&#58;//landregistry.data.gov.uk/landregistry/query" 
# HTTP encode the query
encoded_query = URI.encode_www_form&#40; query&#58; test_query &#41;
# construct the URI object
uri = URI&#40; sparql_endpoint &#41;
uri.query = encoded_query
# show the URL we are about to call, for debugging only
puts uri.inspect
# now send the query
response = Net&#58;&#58;HTTP.get_response&#40;uri&#41;
puts "Response code #&#123;response.code&#125;"
puts response.body
puts "here we go"
json = JSON.parse&#40; response.body &#41;
#s = json&#91;"results"&#93;&#91;"bindings"&#93;&#91;0&#93;&#91;"s"&#93;&#91;"value"&#93;
#puts "s = #&#123;s&#125;"
data_hash = JSON.parse&#40;response.body&#41;
#data_hash&#91;"results"&#93;&#91;"bindings"&#93;.each do |child|
#	puts "text = #&#123;&#40;child&#91;"ppd_pricePaid"&#93;&#91;"value"&#93;&#41;&#125;"

testObject = Sparqlresult.new&#40;data_hash&#41;
testObject.display
value = testObject.avgeDetached

puts value
#end

I've since built an appropriate query in Ebase.
http://landregistry.data.gov.uk/landreg ... %20?amount
0 x


Who is online

Users browsing this forum: No registered users and 18 guests