Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active December 26, 2015 23:59
Show Gist options
  • Save CliffordAnderson/7234215 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/7234215 to your computer and use it in GitHub Desktop.
Converts geographic entities from the Alchemy API to GeoJSON
(: Extracts entities from web-accessible texts using the Alchemy api :)
(: Serializes Disambiguated Entities to GeoJSON :)
xquery version "3.0";
declare namespace csv = "http://basex.org/modules/json";
let $api-key := [Your API Key]
let $url:= "https%3A%2F%2Fraw.github.com%2Fiulibdcs%2Ftei_text%2Fmaster%2Fvwwp_text%2FVAB7013.txt"
let $csv :=
<json type="object">
<type>FeatureCollection</type>
<features type="array">
{
let $entities :=
http:send-request(
<http:request method='post' href='http://access.alchemyapi.com/calls/url/URLGetRankedNamedEntities'>
<http:body media-type='application/x-www-form-urlencoded' method='text'>apikey={$api-key}&amp;url={$url}</http:body>
</http:request>
)/results/entities
for $entity in $entities/entity
where $entity//geo
return
<feature type="object">
<type>Feature</type>
<properties type="object">
{($entity/text)}
</properties>
<geometry type="object">
<type>Point</type>
<coordinates type="array">
<coordinates>{fn:substring-after($entity/disambiguated/geo, " ")}</coordinates>
<coordinates>{fn:substring-before($entity/disambiguated/geo, " ")}</coordinates>
</coordinates>
</geometry>
</feature>
}</features></json>
return json:serialize($csv)
@CliffordAnderson
Copy link
Author

Sample JSON Output


{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "text": "London"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          "-0.12472222222222222",
          "51.50805555555556"
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "text": "Indiana University"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          "-86.17666666666666",
          "39.775555555555556"
        ]
      }
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment