如何编写SPARQL查询获取指定距离内的DBpedia文化历史地点
I want to write a SPARQL query on DBPedia to find nearby cultural-historic sites based on given coordinates. I found an example query that retrieves locations within 20 km of New York City:
SELECT DISTINCT ?resource ?label ?location WHERE { <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo . ?resource geo:geometry ?location ; rdfs:label ?label . FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) . FILTER( lang( ?label ) = "en" ) }
I need to modify this query so that it lists all cultural-historic sites within a custom distance (like the 20km in the example). How can I achieve this?
Answer
Great question! Let's tweak your existing query to meet both requirements—customizable distance and filtering for cultural-historic sites—with clear, maintainable changes.
1. Add a Customizable Radius Parameter
First, replace the hardcoded 20 in the spatial filter with a variable you can set once at the top of the query. Using BIND makes it super easy to adjust the distance later:
BIND(20 AS ?radius) # Change this number to your desired km distance
Now you only need to edit one line to switch between 10km, 30km, or any other range.
2. Filter for Cultural-Historic Site Types
Your original query pulls all locations with geometry data, which includes parks, businesses, etc. To narrow it to cultural-historic sites, we'll add a triple that links ?resource to relevant DBPedia ontology classes. Common options are:
dbo:CulturalHeritageSite(official cultural heritage listings)dbo:HistoricSite(historic locations of note)dbo:Monument(statues, memorials, and monuments)dbo:ArchaeologicalSite(archaeological digs or ruins)
Use a VALUES clause to include multiple types cleanly—this is more readable than chaining UNION statements.
Final Modified Query
Here's the complete, flexible query that combines both changes:
SELECT DISTINCT ?resource ?label ?location WHERE { # Set your desired search radius (in kilometers) here BIND(20 AS ?radius) # Replace this with your target location's DBPedia resource <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo . # Filter for cultural-historic site categories ?resource rdf:type ?siteType ; geo:geometry ?location ; rdfs:label ?label . VALUES ?siteType { dbo:CulturalHeritageSite dbo:HistoricSite dbo:Monument dbo:ArchaeologicalSite } # Spatial filter using our custom radius FILTER( bif:st_intersects( ?location, ?sourcegeo, ?radius ) ) # Keep only English labels for readability FILTER( lang( ?label ) = "en" ) }
Bonus: Use Raw Coordinates Instead of a City Resource
If you don't want to rely on a DBPedia city resource, you can input raw latitude/longitude directly using bif:st_point (note: the order is longitude first, then latitude):
SELECT DISTINCT ?resource ?label ?location WHERE { BIND(20 AS ?radius) # Input your coordinates here: bif:st_point(longitude, latitude) BIND(bif:st_point(-74.0060, 40.7128) AS ?sourcegeo) # NYC's coordinates ?resource rdf:type ?siteType ; geo:geometry ?location ; rdfs:label ?label . VALUES ?siteType { dbo:CulturalHeritageSite dbo:HistoricSite dbo:Monument dbo:ArchaeologicalSite } FILTER( bif:st_intersects( ?location, ?sourcegeo, ?radius ) ) FILTER( lang( ?label ) = "en" ) }
Quick Tips
- The
bif:prefix is specific to DBPedia's BigData endpoint—don't remove it, as the spatial functions won't work without it. - Keep
DISTINCTto avoid duplicate results (some sites might be tagged with multiple heritage categories). - Expand the
VALUES ?siteTypelist if you want to include other relevant classes, likedbo:Museumordbo:Castle.
内容的提问来源于stack exchange,提问作者outbeyond

