Sha256: df3bb2bdb470f330847ba6c841d5073e98a0afc9d3f97f67f6bbf3d0d3d9c6e1

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module Geokit
  module Geocoders
    class FCCGeocoder < Geocoder

      private
      # Template method which does the reverse-geocode lookup.
      def self.do_reverse_geocode(latlng)
        latlng=LatLng.normalize(latlng)
        res = self.call_geocoder_service("http://data.fcc.gov/api/block/find?format=json&latitude=#{Geokit::Inflector::url_escape(latlng.lat.to_s)}&longitude=#{Geokit::Inflector::url_escape(latlng.lng.to_s)}")
        return GeoLoc.new unless (res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPOK))
        json = res.body
        logger.debug "FCC reverse-geocoding. LL: #{latlng}. Result: #{json}"
        return self.json2GeoLoc(json)
      end

      # Template method which does the geocode lookup.
      #
      # ==== EXAMPLES
      # ll=Geokit::LatLng.new(40, -85)
      # Geokit::Geocoders::FCCGeocoder.geocode(ll) #

      # JSON result looks like this
      # => {"County"=>{"name"=>"Wayne", "FIPS"=>"18177"},
      # "Block"=>{"FIPS"=>"181770103002004"},
      # "executionTime"=>"0.099",
      # "State"=>{"name"=>"Indiana", "code"=>"IN", "FIPS"=>"18"},
      # "status"=>"OK"}

      def self.json2GeoLoc(json, address="")
        ret = nil
        results = MultiJson.load(json)

        if results.has_key?('Err') and results['Err']["msg"] == 'There are no results for this location'
          return GeoLoc.new
        end
        # this should probably be smarter.
        if !results['status'] == 'OK'
          raise Geokit::Geocoders::GeocodeError
        end

        res = GeoLoc.new
        res.provider      = 'fcc'
        res.success       = true
        res.precision     = 'block'
        res.country_code  = 'US'
        res.district      = results['County']['name']
        res.district_fips = results['County']['FIPS']
        res.state         = results['State']['code']
        res.state_fips    = results['State']['FIPS']
        res.block_fips    = results['Block']['FIPS']

        res
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geokit-1.7.1 lib/geokit/services/fcc.rb