<> a <http://www.w3.org/2000/10/swap/log#N3Document> .
@prefix dc: <http://purl.org/dc/elements/1.1/>.

@prefix s: <http://www.w3.org/2000/01/rdf-schema#> .

<> dc:title "U.S. Gazetteer rules: place names/zips to lat/long";
  dc:description "$Id: usPlace2LatLong.n3,v 1.3 2008/03/02 23:03:36 timbl Exp $";
  dc:source <http://www.census.gov/cgi-bin/gazetteer>.

@prefix r: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix ont:  <http://www.daml.org/2001/03/daml+oil#> .

@prefix p: <http://www.w3.org/2000/10/swap/pim/usps#>.
@prefix map: <http://www.w3.org/2000/10/swap/pim/earthMap#>.

@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix str: <http://www.w3.org/2000/10/swap/string#>.
@prefix math: <http://www.w3.org/2000/10/swap/math#>.

@prefix : <?#>.

@forAll :where, :cityN, :stN, :i, :pg, :txt.


{ :where p:cityName :cityN;
    [] [ p:stateAbbr :stN ].

  #@@BUG: need to URI-encode :cityN and :stN

  ("http://www.census.gov/cgi-bin/gazetteer?city=" :cityN "&state=" :stN )
     str:concatenation :i.
  :pg log:uri :i.
}
  log:implies {
    :where a map:Marker; map:label :cityN;
	:_latLongPage :pg
  }.

[ :directionName "N";  :multiplier "1"].
[ :directionName "S"; :multiplier "-1"].
[ :directionName "E"; :multiplier "1"].
[ :directionName "W"; :multiplier "-1"].

{
  :where :_latLongPage :pg.
  :pg log:content :txt.
  (:txt "Location: (\\d+\\.\\d+) ") str:scrape :latMag.

  (:txt "Location: \\d+\\.\\d+ (\\w), ")
	str:scrape [ is :directionName of [ :multiplier :latX]].

  (:txt "Location: \\d+\\.\\d+ \\w, (\\d+\\.\\d+)") str:scrape :longMag.

  (:txt "Location: \\d+\\.\\d+ \\w, \\d+\\.\\d+ (\\w)")
	str:scrape [ is :directionName of [ :multiplier :longX]].

  (:latMag :latX) math:product :lat.
  (:longMag :longX) math:product :long.
}
  log:implies {
    :where map:x :long; map:y :lat; :_latMag :latMag; :_latX :latX.
  }.

@forAll :lat, :latMag, :latX, :long, :longMag, :longX.


# test data...
     @prefix g: <http://www.cyc.com/cyc-2-1/vocab/spatial-vocab.html#> .
      [      g:inRegion  [
                 p:cityName "Wilder";
                 g:inRegion  [
                     p:stateAbbr "KY" ] ] ].
