Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Turner http://highearthorbit.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Location queries for a model

// These are class functions which are useful for finding locations in a database
// when PostGIS is not available (when using MySQL for example)

def self.deg2rad(deg)
	(deg * Math::PI / 180)
end

def self.rad2deg(rad)
	(rad * 180 / Math::PI)
end

def self.acos(rad)
	Math.atan2(Math.sqrt(1 - rad**2), rad)
end

def self.distance_in_miles(loc1, loc2)
	lat1 = loc1.latitude
	lon1 = loc1.longitude
	lat2 = loc2.latitude
	lon2 = loc2.longitude
	theta = lon1 - lon2
	
	dist = Math.sin(self.deg2rad(lat1)) * Math.sin(deg2rad(lat2)) 
		+ Math.cos(self.deg2rad(lat1)) * Math.cos(self.deg2rad(lat2)) * Math.cos(deg2rad(theta))
	
	dist = self.rad2deg(self.acos(dist))
	
	(dist * 60 * 1.1515).round #distance in miles
end

def miles_to(location)
	Location.distance_in_miles(self, location)
end

def self.locationArea(location, miles)
	radius = miles.to_f
	latR = radius / ((6076 / 5280) * 60)
	lonR = radius / (((Math.cos(location.latitude * Math::PI / 180) * 6076) / 5280) * 60)
	
	{
		:min_latitude => location.latitude - latR,
		:min_longitude => location.longitude - lonR,
		:max_latitude => location.latitude + latR,
		:max_longitude => location.longitude + lonR
	}
end

def self.location_ids_in_range(location, miles)
	la = Location.locationArea(location, miles)
	Location.find(:all,
		:select => "locations.id",
		:conditions => ["locations.latitude <= ? and locations.latitude >= ? " + \
			" AND locations.longitude >= ? and locations.longitude <= ?",
			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]
		]
	).collect { |l| l.id }
end

def self.locations_in_range(location, miles)
	la = Location.locationArea(location, miles)
	Location.find(:all,
		:conditions => ["locations.latitude <= ? and locations.latitude >= ? " + \
			" AND locations.longitude >= ? and locations.longitude <= ?",
			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]
		]
	)
end

Export a GoogleEarth KML file to CSV output

# Parses a GoogleEarth KML file and writes the pertinent data to a CSV file

#<?xml version="1.0" encoding="UTF-8"?>
# <kml xmlns="http://earth.google.com/kml/2.0">
# <Folder>
# <name>Folder Name</name>
# <open>1</open>
# <ScreenOverlay>
# ... Screen Overlays
# </ScreenOverlay>
# <Document>
# <name>Document Name</name>
# <open>1</open>
# <Schema parent="Placemark" name="S_Parcel_centroids_SSSS">
# ... Schema Def ...
# </Schema>
# <Style id="khStyle722">
# ... Style Info ...
# </Style>
# <Folder id="layer 0">
# <name>FolderName</name>
# <Location>
# <name>Location name 1</name>
# <description><![CDATA[...Description text...]]></description>
# <styleUrl>#khStyle16037</styleUrl>
# <Point>
# <altitudeMode>relativeToGround
# <coordinates>-81.85829063169155,29.12257052899974,100</coordinates>
# </Point>
# <Column1>Column 1 data</Column1>
# <Column2>Column 2 data</Column2>
# </Location>
# <Location>
# <name>Location name 2</name>
# <description><![CDATA[...Description text...]]></description>
# <styleUrl>#khStyle16037</styleUrl>
# <Point>
# <altitudeMode>relativeToGround
# <coordinates>-81.85829063169155,29.12257052899974,100</coordinates>
# </Point>
# <Column1>Column 1 data</Column1>
# <Column2>Column 2 data</Column2>
# </Location>
#
# ... More Locations ...
# </Folder>
# </Document>
# </Folder>
# </kml>


require "rexml/document"
include REXML
kmlroot = (Document.new File.new "data.kml").root
nodes = kmlroot.elements.to_a("//Location")

begin
	f = File.open("data.csv", "w")
	f << "id,column1,column2,latitude,longitude"\n"
	id = 1
	nodes.each { |node|
		column1 = node.elements["column1"].text
		column2 = node.elements["column2"].text
		coords = node.elements["Point"].elements["coordinates"].text.split(",")
		f << [id, column1, column2, coords[1], coords[0] ].join(",") << "\n"
		id += 1
	}
ensure
	f.close
end

US State Abbreviations to Full name

# US State abbreviations to full name
# state_name = state_abbr.[]("MI")

state_abbr = {
  'AL' => 'Alabama',
  'AK' => 'Alaska',
  'AS' => 'America Samoa',
  'AZ' => 'Arizona',
  'AR' => 'Arkansas',
  'CA' => 'California',
  'CO' => 'Colorado',
  'CT' => 'Connecticut',
  'DE' => 'Delaware',
  'DC' => 'District of Columbia',
  'FM' => 'Micronesia1',
  'FL' => 'Florida',
  'GA' => 'Georgia',
  'GU' => 'Guam',
  'HI' => 'Hawaii',
  'ID' => 'Idaho',
  'IL' => 'Illinois',
  'IN' => 'Indiana',
  'IA' => 'Iowa',
  'KS' => 'Kansas',
  'KY' => 'Kentucky',
  'LA' => 'Louisiana',
  'ME' => 'Maine',
  'MH' => 'Islands1',
  'MD' => 'Maryland',
  'MA' => 'Massachusetts',
  'MI' => 'Michigan',
  'MN' => 'Minnesota',
  'MS' => 'Mississippi',
  'MO' => 'Missouri',
  'MT' => 'Montana',
  'NE' => 'Nebraska',
  'NV' => 'Nevada',
  'NH' => 'New Hampshire',
  'NJ' => 'New Jersey',
  'NM' => 'New Mexico',
  'NY' => 'New York',
  'NC' => 'North Carolina',
  'ND' => 'North Dakota',
  'OH' => 'Ohio',
  'OK' => 'Oklahoma',
  'OR' => 'Oregon',
  'PW' => 'Palau',
  'PA' => 'Pennsylvania',
  'PR' => 'Puerto Rico',
  'RI' => 'Rhode Island',
  'SC' => 'South Carolina',
  'SD' => 'South Dakota',
  'TN' => 'Tennessee',
  'TX' => 'Texas',
  'UT' => 'Utah',
  'VT' => 'Vermont',
  'VI' => 'Virgin Island',
  'VA' => 'Virginia',
  'WA' => 'Washington',
  'WV' => 'West Virginia',
  'WI' => 'Wisconsin',
  'WY' => 'Wyoming'
}

Average US State Locations

# A hash table of average US State Locations (latitude, longitude)
# MI_Location = state_locations.[]("MI")

state_locations = {
  'AK' => [61.3850,-152.2683],
  'AL' => [32.7990,-86.8073],
  'AR' => [34.9513,-92.3809],
  'AS' => [14.2417,-170.7197],
  'AZ' => [33.7712,-111.3877],
  'CA' => [36.1700,-119.7462],
  'CO' => [39.0646,-105.3272],
  'CT' => [41.5834,-72.7622],
  'DC' => [38.8964,-77.0262],
  'DE' => [39.3498,-75.5148],
  'FL' => [27.8333,-81.7170],
  'GA' => [32.9866,-83.6487],
  'HI' => [21.1098,-157.5311],
  'IA' => [42.0046,-93.2140],
  'ID' => [44.2394,-114.5103],
  'IL' => [40.3363,-89.0022],
  'IN' => [39.8647,-86.2604],
  'KS' => [38.5111,-96.8005],
  'KY' => [37.6690,-84.6514],
  'LA' => [31.1801,-91.8749],
  'MA' => [42.2373,-71.5314],
  'MD' => [39.0724,-76.7902],
  'ME' => [44.6074,-69.3977],
  'MI' => [43.3504,-84.5603],
  'MN' => [45.7326,-93.9196],
  'MO' => [38.4623,-92.3020],
  'MP' => [14.8058,145.5505],
  'MS' => [32.7673,-89.6812],
  'MT' => [46.9048,-110.3261],
  'NC' => [35.6411,-79.8431],
  'ND' => [47.5362,-99.7930],
  'NE' => [41.1289,-98.2883],
  'NH' => [43.4108,-71.5653],
  'NJ' => [40.3140,-74.5089],
  'NM' => [34.8375,-106.2371],
  'NV' => [38.4199,-117.1219],
  'NY' => [42.1497,-74.9384],
  'OH' => [40.3736,-82.7755],
  'OK' => [35.5376,-96.9247],
  'OR' => [44.5672,-122.1269],
  'PA' => [40.5773,-77.2640],
  'PR' => [18.2766,-66.3350],
  'RI' => [41.6772,-71.5101],
  'SC' => [33.8191,-80.9066],
  'SD' => [44.2853,-99.4632],
  'TN' => [35.7449,-86.7489],
  'TX' => [31.1060,-97.6475],
  'UT' => [40.1135,-111.8535],
  'VA' => [37.7680,-78.2057],
  'VI' => [18.0001,-64.8199],
  'VT' => [44.0407,-72.7093],
  'WA' => [47.3917,-121.5708],
  'WI' => [44.2563,-89.6385],
  'WV' => [38.4680,-80.9696],
  'WY' => [42.7475,-107.2085]
}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS