<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: location code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 19:13:56 GMT</pubDate>
    <description>DZone Snippets: location code</description>
    <item>
      <title>Programmatically Change a Crystal Reports datasource location</title>
      <link>http://snippets.dzone.com/posts/show/4029</link>
      <description>I had an aspx.net project where I needed to generate a pdf.&lt;br /&gt;I used crystal to do the heavy lifting of geneating the pdf for me.&lt;br /&gt;But as I needed to deploy it to multiple databases/clients I needed a way of programmatically changing the datasource location of the report.  So with a bit of help from the crystal knowledgebase here is the code I use.&lt;br /&gt;There is a lot of debugging information in here and trust me when it fails you need it.&lt;br /&gt;The .dbo of the location reference obviously means I was working with sql server.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' SetupReport&lt;br /&gt;   '&lt;br /&gt;   ' Description:&lt;br /&gt;   '    sets up the crystal report&lt;br /&gt;   '&lt;br /&gt;   ' Arguments:&lt;br /&gt;   '    objCrystalReportDocument             - crystal report document object&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    GetConnnectionInfo&lt;br /&gt;   '    CrystalDescisions&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 03/17/2006 - WSR : created&lt;br /&gt;   '&lt;br /&gt;   Private Function SetupReport(ByRef objCrystalReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument) As System.Boolean&lt;br /&gt;&lt;br /&gt;      ' a heck of a lot of objects used&lt;br /&gt;      Dim crParameterDiscreteValue As CrystalDecisions.Shared.ParameterDiscreteValue&lt;br /&gt;      Dim crParameterFieldDefinitions As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions&lt;br /&gt;      Dim crParameterFieldLocation As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition&lt;br /&gt;      Dim crParameterValues As CrystalDecisions.Shared.ParameterValues&lt;br /&gt;      Dim crSections As CrystalDecisions.CrystalReports.Engine.Sections&lt;br /&gt;      Dim crSection As CrystalDecisions.CrystalReports.Engine.Section&lt;br /&gt;      Dim crReportObjects As CrystalDecisions.CrystalReports.Engine.ReportObjects&lt;br /&gt;      Dim crReportObject As CrystalDecisions.CrystalReports.Engine.ReportObject&lt;br /&gt;      Dim crSubreportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument&lt;br /&gt;      Dim crSubreportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject&lt;br /&gt;      Dim crConnectionInfo As CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;      Dim crDatabase As CrystalDecisions.CrystalReports.Engine.Database&lt;br /&gt;      Dim crTables As CrystalDecisions.CrystalReports.Engine.Tables&lt;br /&gt;      Dim aTable As CrystalDecisions.CrystalReports.Engine.Table&lt;br /&gt;      Dim bTable As CrystalDecisions.CrystalReports.Engine.Table&lt;br /&gt;      Dim crTableLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo&lt;br /&gt;      Dim blnTest As System.Boolean&lt;br /&gt;      Dim strLocation As System.String&lt;br /&gt;      Dim blnErrors As System.Boolean&lt;br /&gt;&lt;br /&gt;      ' instantiate the debug page&lt;br /&gt;      m_strDebugPage = New System.Text.StringBuilder(4096)&lt;br /&gt;      blnErrors = False&lt;br /&gt;&lt;br /&gt;      crConnectionInfo = GetConnectionInfo()&lt;br /&gt;&lt;br /&gt;      crDatabase = objCrystalReportDocument.Database&lt;br /&gt;&lt;br /&gt;      crTables = crDatabase.Tables&lt;br /&gt;&lt;br /&gt;      'For intCounter = 0 To objCrystalReportDocument.Database.Tables.Count - 1&lt;br /&gt;      For Each aTable In crTables&lt;br /&gt;&lt;br /&gt;         crTableLogOnInfo = aTable.LogOnInfo&lt;br /&gt;&lt;br /&gt;         OutputDebugLine("BEFORE")&lt;br /&gt;         OutputDebugLine("TABLE NAME: " &amp; aTable.Name)&lt;br /&gt;         OutputDebugLine("TABLE LOC: " &amp; aTable.Location)&lt;br /&gt;         OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;         OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;         OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;         OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;         OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;         OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;&lt;br /&gt;         crTableLogOnInfo.ConnectionInfo = crConnectionInfo&lt;br /&gt;         aTable.ApplyLogOnInfo(crTableLogOnInfo)&lt;br /&gt;         strLocation = crConnectionInfo.DatabaseName &amp; ".dbo." &amp; aTable.Location.Substring(aTable.Location.LastIndexOf(".") + 1)&lt;br /&gt;         OutputDebugLine("New Location: " &amp; strLocation)&lt;br /&gt;         Try&lt;br /&gt;            aTable.Location = strLocation&lt;br /&gt;         Catch ex As Exception&lt;br /&gt;            OutputDebugLine("Set Location Error: " &amp; ex.ToString)&lt;br /&gt;            blnErrors = True&lt;br /&gt;         End Try&lt;br /&gt;&lt;br /&gt;         OutputDebugLine("AFTER")&lt;br /&gt;         OutputDebugLine("TABLE NAME: " &amp; aTable.Name)&lt;br /&gt;         OutputDebugLine("TABLE LOC: " &amp; aTable.Location)&lt;br /&gt;         OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;         OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;         OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;         OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;         OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;         OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;         Try&lt;br /&gt;            blnTest = aTable.TestConnectivity()&lt;br /&gt;            OutputDebugLine("CONNECTED? " &amp; blnTest.ToString())&lt;br /&gt;         Catch ex As Exception&lt;br /&gt;            OutputDebugLine("CONNECTED? NO")&lt;br /&gt;            OutputDebugLine(ex.ToString)&lt;br /&gt;            blnErrors = True&lt;br /&gt;         End Try&lt;br /&gt;&lt;br /&gt;         '// THIS STUFF HERE IS FOR REPORTS HAVING SUBREPORTS &lt;br /&gt;         '// set the sections object to the current report's section &lt;br /&gt;         crSections = objCrystalReportDocument.ReportDefinition.Sections&lt;br /&gt;&lt;br /&gt;         '// loop through all the sections to find all the report objects &lt;br /&gt;         For Each crSection In crSections&lt;br /&gt;&lt;br /&gt;            crReportObjects = crSection.ReportObjects&lt;br /&gt;&lt;br /&gt;            '//loop through all the report objects in there to find all subreports &lt;br /&gt;            For Each crReportObject In crReportObjects&lt;br /&gt;&lt;br /&gt;               If crReportObject.Kind = ReportObjectKind.SubreportObject Then&lt;br /&gt;&lt;br /&gt;                  crSubreportObject = CType(crReportObject, CrystalDecisions.CrystalReports.Engine.SubreportObject)&lt;br /&gt;                  '//open the subreport object and logon as for the general report &lt;br /&gt;                  crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName)&lt;br /&gt;                  crDatabase = crSubreportDocument.Database&lt;br /&gt;                  crTables = crDatabase.Tables&lt;br /&gt;&lt;br /&gt;                  For Each bTable In crTables&lt;br /&gt;&lt;br /&gt;                     crTableLogOnInfo = bTable.LogOnInfo&lt;br /&gt;&lt;br /&gt;                     OutputDebugLine("BEFORE")&lt;br /&gt;                     OutputDebugLine("TABLE NAME: " &amp; bTable.Name)&lt;br /&gt;                     OutputDebugLine("TABLE LOC: " &amp; bTable.Location)&lt;br /&gt;                     OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;                     OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;                     OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;                     OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;                     OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;                     OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;&lt;br /&gt;                     crTableLogOnInfo.ConnectionInfo = crConnectionInfo&lt;br /&gt;                     bTable.ApplyLogOnInfo(crTableLogOnInfo)&lt;br /&gt;                     strLocation = crConnectionInfo.DatabaseName &amp; ".dbo." &amp; bTable.Location.Substring(bTable.Location.LastIndexOf(".") + 1)&lt;br /&gt;                     OutputDebugLine("New Location: " &amp; strLocation)&lt;br /&gt;                     Try&lt;br /&gt;                        bTable.Location = strLocation&lt;br /&gt;                     Catch ex As Exception&lt;br /&gt;                        OutputDebugLine("Set Location Error: " &amp; ex.ToString)&lt;br /&gt;                        blnErrors = True&lt;br /&gt;                     End Try&lt;br /&gt;&lt;br /&gt;                     OutputDebugLine("AFTER")&lt;br /&gt;                     OutputDebugLine("TABLE NAME: " &amp; bTable.Name)&lt;br /&gt;                     OutputDebugLine("TABLE LOC: " &amp; bTable.Location)&lt;br /&gt;                     OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;                     OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;                     OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;                     OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;                     OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;                     OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;                     Try&lt;br /&gt;                        blnTest = bTable.TestConnectivity()&lt;br /&gt;                        OutputDebugLine("CONNECTED? " &amp; blnTest.ToString())&lt;br /&gt;                     Catch ex As Exception&lt;br /&gt;                        OutputDebugLine("CONNECTED? NO")&lt;br /&gt;                        OutputDebugLine(ex.ToString)&lt;br /&gt;                        blnErrors = True&lt;br /&gt;                     End Try&lt;br /&gt;&lt;br /&gt;                  Next bTable&lt;br /&gt;&lt;br /&gt;               End If&lt;br /&gt;&lt;br /&gt;            Next crReportObject&lt;br /&gt;&lt;br /&gt;         Next crSection&lt;br /&gt;&lt;br /&gt;      Next aTable&lt;br /&gt;&lt;br /&gt;      ' get parameter fields from report&lt;br /&gt;      crParameterFieldDefinitions = objCrystalReportDocument.DataDefinition.ParameterFields&lt;br /&gt;&lt;br /&gt;      '    ' Set the first parameter&lt;br /&gt;      '    ' - Get the parameter, tell it to use the current values vs default value.&lt;br /&gt;      '    ' - Tell it the parameter contains 1 discrete value vs multiple values.&lt;br /&gt;      '    ' - Set the parameter's value.&lt;br /&gt;      '    ' - Add it and apply it.&lt;br /&gt;      '    ' - Repeat these statements for each parameter.&lt;br /&gt;      '    '&lt;br /&gt;      crParameterFieldLocation = crParameterFieldDefinitions.Item("@psindex")&lt;br /&gt;      crParameterValues = crParameterFieldLocation.CurrentValues&lt;br /&gt;      crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue&lt;br /&gt;      crParameterDiscreteValue.Value = m_intReportID&lt;br /&gt;      crParameterValues.Add(crParameterDiscreteValue)&lt;br /&gt;      crParameterFieldLocation.ApplyCurrentValues(crParameterValues)&lt;br /&gt;&lt;br /&gt;      ' if there were errors&lt;br /&gt;      If blnErrors Then&lt;br /&gt;&lt;br /&gt;         ' display debug page&lt;br /&gt;         OutputDebugPage()&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' GetConnectionInfo&lt;br /&gt;   '&lt;br /&gt;   ' Description:&lt;br /&gt;   '    retrieves the connection information from the data layer object&lt;br /&gt;   '&lt;br /&gt;   ' Arguments: none&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    DataLayer.GetConnectInfo&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 03/17/2006 - WSR : created&lt;br /&gt;   '&lt;br /&gt;   Private Function GetConnectionInfo() As CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;&lt;br /&gt;      Dim objConnectionInfo As CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;      Dim strDSN As System.String&lt;br /&gt;      Dim strDB As System.String&lt;br /&gt;      Dim strUID As System.String&lt;br /&gt;      Dim strPWD As System.String&lt;br /&gt;      Dim blnTrust As System.Boolean&lt;br /&gt;&lt;br /&gt;      ' get connection information from data layer&lt;br /&gt;      m_objDataLayer.GetConnectInfo(strDSN, strDB, strUID, strPWD, blnTrust)&lt;br /&gt;&lt;br /&gt;      ' create new crystal connection info object&lt;br /&gt;      objConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;&lt;br /&gt;      ' populate it&lt;br /&gt;      objConnectionInfo.IntegratedSecurity = False&lt;br /&gt;      objConnectionInfo.ServerName = strDSN&lt;br /&gt;      objConnectionInfo.UserID = strUID&lt;br /&gt;      objConnectionInfo.Password = strPWD&lt;br /&gt;      objConnectionInfo.DatabaseName = strDB&lt;br /&gt;&lt;br /&gt;      ' return object&lt;br /&gt;      GetConnectionInfo = objConnectionInfo&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' OutputDebugLine&lt;br /&gt;   '&lt;br /&gt;   ' Description: appends a line to the debug string builder&lt;br /&gt;   '&lt;br /&gt;   ' Arguments: text to add&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    m_strDebugPage&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 03/17/2006 - WSR : created&lt;br /&gt;   ' 2007.04.25 - WSR : revised to use string builder&lt;br /&gt;   '&lt;br /&gt;   Function OutputDebugLine(ByVal strLine As System.String) As System.Boolean&lt;br /&gt;&lt;br /&gt;      m_strDebugPage.Append("&lt;div&gt;" &amp; Server.HtmlEncode(strLine) &amp; "&lt;/div&gt;")&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' OutputDebugPage&lt;br /&gt;   '&lt;br /&gt;   ' Description: sends debug string builder to response&lt;br /&gt;   '&lt;br /&gt;   ' Arguments: none&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    m_strDebugPage&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 2007.04.25 - WSR : created&lt;br /&gt;   '&lt;br /&gt;   Function OutputDebugPage() As System.Boolean&lt;br /&gt;&lt;br /&gt;      With Response&lt;br /&gt;         .ClearHeaders()&lt;br /&gt;         .ClearContent()&lt;br /&gt;         .ContentType = "text/html"&lt;br /&gt;      End With&lt;br /&gt;&lt;br /&gt;      Response.Write("&lt;html&gt;&lt;head&gt;&lt;title&gt;Debug Page&lt;/title&gt;&lt;/head&gt;&lt;body&gt;")&lt;br /&gt;      Response.Write(m_strDebugPage.ToString())&lt;br /&gt;      Response.Write("&lt;/body&gt;&lt;/html&gt;")&lt;br /&gt;&lt;br /&gt;      Response.Flush()&lt;br /&gt;      Response.End()&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 May 2007 20:30:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4029</guid>
      <author>Will_Rickards (Will Rickards)</author>
    </item>
    <item>
      <title>IP Location on Google Maps</title>
      <link>http://snippets.dzone.com/posts/show/3814</link>
      <description>/*&lt;br /&gt;Example of usage:&lt;br /&gt;$ ./lip.rb snippets.dzone.com&lt;br /&gt;. Hostname: snippets.dzone.com&lt;br /&gt;. Country Code: US&lt;br /&gt;. Country Name: United States&lt;br /&gt;. Region: CA&lt;br /&gt;. Region Name: California&lt;br /&gt;. City: Los Angeles&lt;br /&gt;. Postal Code: 90017&lt;br /&gt;. Latitude: 34.0530&lt;br /&gt;. Longitude: -118.2642&lt;br /&gt;. ISP: CoreExpress&lt;br /&gt;. Organization: CoreExpress&lt;br /&gt;. Metro Code: 803&lt;br /&gt;. Area Code: 213&lt;br /&gt;. Google Maps URL: http://maps.google.com/maps?q=34.0530,+-118.2642&amp;iwloc=A&amp;hl=en&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -w&lt;br /&gt;&lt;br /&gt;if ARGV.empty?&lt;br /&gt;  puts &lt;&lt;-T&lt;br /&gt;Locate IP by haqu&lt;br /&gt;usage: ./lip.rb ip|domain ...&lt;br /&gt;  T&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'uri'&lt;br /&gt;&lt;br /&gt;uri = URI.parse('http://www.maxmind.com/app/locate_ip')&lt;br /&gt;res = Net::HTTP.post_form(uri,&lt;br /&gt;  { 'ips' =&gt; ARGV.join(' '),&lt;br /&gt;    'type' =&gt; '', 'u' =&gt; '', 'p' =&gt; ''&lt;br /&gt;  } )&lt;br /&gt;fstr = res.body&lt;br /&gt;&lt;br /&gt;fstr.gsub!("Edition Results&lt;\/span&gt;&lt;p&gt;","CHECKPOINT")&lt;br /&gt;fstr =~ /CHECKPOINT(.+?)&lt;\/table&gt;/m&lt;br /&gt;fields = $1.grep(/&lt;(th|td)&gt;/)&lt;br /&gt;fields.each do |f|&lt;br /&gt;  f.strip!&lt;br /&gt;  f.gsub!(/&lt;[^&gt;]+&gt;/,"")&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;(0...13).each do |i|&lt;br /&gt;  puts ". #{fields[i]}: #{fields[i+13]}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;maplink = "http://maps.google.com/maps?q=#{fields[20]},+#{fields[21]}&amp;iwloc=A&amp;hl=en"&lt;br /&gt;puts ". Google Maps URL: #{maplink}"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 14 Apr 2007 13:33:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3814</guid>
      <author>haqu (Sergey)</author>
    </item>
    <item>
      <title>Which class file is loaded by the classloader ?</title>
      <link>http://snippets.dzone.com/posts/show/3719</link>
      <description>When using lots of third-party libraries, one problem might be that 2 of them package different versions of the same class, producing errors when method version conflicts happen.&lt;br /&gt;&lt;br /&gt;Here is a simple way to know the exact location used by the classloader to get your class :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;URL myClassURL = MyMysteryClass.class.getProtectionDomain().getCodeSource().getLocation();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Mar 2007 11:38:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3719</guid>
      <author>nivek (Kevin Gaudin)</author>
    </item>
    <item>
      <title>Response::redirect //PHP Function</title>
      <link>http://snippets.dzone.com/posts/show/2449</link>
      <description>&lt;code&gt;&lt;br /&gt;class Response{&lt;br /&gt;	function redirect($url){&lt;br /&gt;		exit(header('Location: ' . $url));&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 20 Aug 2006 21:28:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2449</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>gps gsm location python s60</title>
      <link>http://snippets.dzone.com/posts/show/2029</link>
      <description>Hi Guys,&lt;br /&gt;&lt;br /&gt;I've just put together two smaller Python apps I've seen around in this discussion board.&lt;br /&gt;&lt;br /&gt;The resulting app prints 1) info obtained by a BT gps (i.e. $GPRMC sentence, but may change as you like) and 2) GSM cell id.&lt;br /&gt;&lt;br /&gt;My wish is to collect these info periodically (i.e. 2/3 minutes) and send them back via an HTTP POST to a specified host.... could anybody help? ;-)&lt;br /&gt;&lt;br /&gt;Thanks&lt;br /&gt;&lt;br /&gt;-Luca&lt;br /&gt;&lt;br /&gt;____________________________________&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# Simple BT App&lt;br /&gt;#$GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13,309.62,120598, ,*10&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import socket,location,urllib&lt;br /&gt;&lt;br /&gt;class BTReader:&lt;br /&gt;&lt;br /&gt;def connect(self):&lt;br /&gt;self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)&lt;br /&gt;address,services=socket.bt_discover()&lt;br /&gt;print "Discovered: %s, %s"%(address,services)&lt;br /&gt;target=(address,services.values()[0])&lt;br /&gt;print "Connecting to "+str(target)&lt;br /&gt;self.sock.connect(target)&lt;br /&gt;&lt;br /&gt;def readposition(self):&lt;br /&gt;try:&lt;br /&gt;buffer=""&lt;br /&gt;ch = self.sock.recv(1)&lt;br /&gt;while(ch !='\n'):&lt;br /&gt;buffer+=ch&lt;br /&gt;ch = self.sock.recv(1)&lt;br /&gt;# print buffer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if (buffer[0:6]=="$GPRMC"):&lt;br /&gt;(GPRMC,utc,status,lat,latns,lon,lonew,knots,course,date,xx1,xx2)=buffer.split(",")&lt;br /&gt;return "GPS (%s,%s,%s,%s,%s)"%(utc,lat+latns,lon+lonew,knots,course)&lt;br /&gt;except Error:&lt;br /&gt;return "Error!\n"&lt;br /&gt;return ""&lt;br /&gt;&lt;br /&gt;def close(self):&lt;br /&gt;self.sock.close()&lt;br /&gt;&lt;br /&gt;class GSM_loc:&lt;br /&gt;&lt;br /&gt;def upd(self):&lt;br /&gt;self.loc = location.gsm_location()&lt;br /&gt;return "GSM (MCC:%s MNC:%s LAC:%s CID=%s)"%(self.loc[0], self.loc[1], self.loc[2], self.loc[3])&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;gsm = GSM_loc()&lt;br /&gt;&lt;br /&gt;bt=BTReader()&lt;br /&gt;bt.connect()&lt;br /&gt;&lt;br /&gt;i=0&lt;br /&gt;while (i&lt;15):&lt;br /&gt;print gsm.upd()&lt;br /&gt;print bt.readposition()&lt;br /&gt;i+=1&lt;br /&gt;&lt;br /&gt;bt.close()</description>
      <pubDate>Mon, 15 May 2006 21:09:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2029</guid>
      <author>ollip1 ()</author>
    </item>
    <item>
      <title>Location queries for a model</title>
      <link>http://snippets.dzone.com/posts/show/1906</link>
      <description>// These are class functions which are useful for finding locations in a database &lt;br /&gt;// when PostGIS is not available (when using MySQL for example)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def self.deg2rad(deg)&lt;br /&gt;	(deg * Math::PI / 180)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.rad2deg(rad)&lt;br /&gt;	(rad * 180 / Math::PI)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.acos(rad)&lt;br /&gt;	Math.atan2(Math.sqrt(1 - rad**2), rad)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.distance_in_miles(loc1, loc2)&lt;br /&gt;	lat1 = loc1.latitude&lt;br /&gt;	lon1 = loc1.longitude&lt;br /&gt;	lat2 = loc2.latitude&lt;br /&gt;	lon2 = loc2.longitude&lt;br /&gt;	theta = lon1 - lon2&lt;br /&gt;	&lt;br /&gt;	dist = Math.sin(self.deg2rad(lat1)) * Math.sin(deg2rad(lat2)) &lt;br /&gt;		+ Math.cos(self.deg2rad(lat1)) * Math.cos(self.deg2rad(lat2)) * Math.cos(deg2rad(theta))&lt;br /&gt;	&lt;br /&gt;	dist = self.rad2deg(self.acos(dist))&lt;br /&gt;	&lt;br /&gt;	(dist * 60 * 1.1515).round #distance in miles&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def miles_to(location)&lt;br /&gt;	Location.distance_in_miles(self, location)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.locationArea(location, miles)&lt;br /&gt;	radius = miles.to_f&lt;br /&gt;	latR = radius / ((6076 / 5280) * 60)&lt;br /&gt;	lonR = radius / (((Math.cos(location.latitude * Math::PI / 180) * 6076) / 5280) * 60)&lt;br /&gt;	&lt;br /&gt;	{&lt;br /&gt;		:min_latitude =&gt; location.latitude - latR,&lt;br /&gt;		:min_longitude =&gt; location.longitude - lonR,&lt;br /&gt;		:max_latitude =&gt; location.latitude + latR,&lt;br /&gt;		:max_longitude =&gt; location.longitude + lonR&lt;br /&gt;	}&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.location_ids_in_range(location, miles)&lt;br /&gt;	la = Location.locationArea(location, miles)&lt;br /&gt;	Location.find(:all,&lt;br /&gt;		:select =&gt; "locations.id",&lt;br /&gt;		:conditions =&gt; ["locations.latitude &lt;= ? and locations.latitude &gt;= ? " + \&lt;br /&gt;			" AND locations.longitude &gt;= ? and locations.longitude &lt;= ?",&lt;br /&gt;			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]&lt;br /&gt;		]&lt;br /&gt;	).collect { |l| l.id }&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.locations_in_range(location, miles)&lt;br /&gt;	la = Location.locationArea(location, miles)&lt;br /&gt;	Location.find(:all,&lt;br /&gt;		:conditions =&gt; ["locations.latitude &lt;= ? and locations.latitude &gt;= ? " + \&lt;br /&gt;			" AND locations.longitude &gt;= ? and locations.longitude &lt;= ?",&lt;br /&gt;			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]&lt;br /&gt;		]&lt;br /&gt;	)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Apr 2006 05:46:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1906</guid>
      <author>ajturner (Andrew Turner)</author>
    </item>
    <item>
      <title>Export a GoogleEarth KML file to CSV output</title>
      <link>http://snippets.dzone.com/posts/show/1765</link>
      <description># Parses a GoogleEarth KML file and writes the pertinent data to a CSV file&lt;br /&gt;&lt;br /&gt;#&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;br /&gt;# &lt;kml xmlns="http://earth.google.com/kml/2.0"&gt;&lt;br /&gt;# &lt;Folder&gt;&lt;br /&gt;#   &lt;name&gt;Folder Name&lt;/name&gt;&lt;br /&gt;#   &lt;open&gt;1&lt;/open&gt;&lt;br /&gt;#   &lt;ScreenOverlay&gt;&lt;br /&gt;# 		... Screen Overlays&lt;br /&gt;#   &lt;/ScreenOverlay&gt;&lt;br /&gt;#   &lt;Document&gt;&lt;br /&gt;#     &lt;name&gt;Document Name&lt;/name&gt;&lt;br /&gt;#     &lt;open&gt;1&lt;/open&gt;&lt;br /&gt;#     &lt;Schema parent="Placemark" name="S_Parcel_centroids_SSSS"&gt;&lt;br /&gt;# 		... Schema Def ...&lt;br /&gt;#     &lt;/Schema&gt;&lt;br /&gt;#     &lt;Style id="khStyle722"&gt;&lt;br /&gt;# 		... Style Info ...&lt;br /&gt;#     &lt;/Style&gt;&lt;br /&gt;#     &lt;Folder id="layer 0"&gt;&lt;br /&gt;#       &lt;name&gt;FolderName&lt;/name&gt;&lt;br /&gt;#       &lt;Location&gt;&lt;br /&gt;# 	      &lt;name&gt;Location name 1&lt;/name&gt;&lt;br /&gt;# 	      &lt;description&gt;&lt;![CDATA[...Description text...]]&gt;&lt;/description&gt;&lt;br /&gt;# 	      &lt;styleUrl&gt;#khStyle16037&lt;/styleUrl&gt;&lt;br /&gt;# 	      &lt;Point&gt;&lt;br /&gt;# 	        &lt;altitudeMode&gt;relativeToGround&lt;/altitudeMode&gt;&lt;br /&gt;# 	        &lt;coordinates&gt;-81.85829063169155,29.12257052899974,100&lt;/coordinates&gt;&lt;br /&gt;# 	      &lt;/Point&gt;&lt;br /&gt;# 	      &lt;Column1&gt;Column 1 data&lt;/Column1&gt;&lt;br /&gt;# 	      &lt;Column2&gt;Column 2 data&lt;/Column2&gt;&lt;br /&gt;#       &lt;/Location&gt;&lt;br /&gt;#       &lt;Location&gt;&lt;br /&gt;#         &lt;name&gt;Location name 2&lt;/name&gt;&lt;br /&gt;#         &lt;description&gt;&lt;![CDATA[...Description text...]]&gt;&lt;/description&gt;&lt;br /&gt;#         &lt;styleUrl&gt;#khStyle16037&lt;/styleUrl&gt;&lt;br /&gt;#         &lt;Point&gt;&lt;br /&gt;#           &lt;altitudeMode&gt;relativeToGround&lt;/altitudeMode&gt;&lt;br /&gt;#           &lt;coordinates&gt;-81.85829063169155,29.12257052899974,100&lt;/coordinates&gt;&lt;br /&gt;#         &lt;/Point&gt;&lt;br /&gt;#         &lt;Column1&gt;Column 1 data&lt;/Column1&gt;&lt;br /&gt;#         &lt;Column2&gt;Column 2 data&lt;/Column2&gt;&lt;br /&gt;#       &lt;/Location&gt;&lt;br /&gt;# 	&lt;br /&gt;# 		... More Locations ...&lt;br /&gt;#    &lt;/Folder&gt;&lt;br /&gt;#   &lt;/Document&gt;&lt;br /&gt;# &lt;/Folder&gt;&lt;br /&gt;# &lt;/kml&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "rexml/document"&lt;br /&gt;include REXML&lt;br /&gt;kmlroot = (Document.new File.new "data.kml").root&lt;br /&gt;nodes = kmlroot.elements.to_a("//Location")&lt;br /&gt;&lt;br /&gt;begin&lt;br /&gt;	f = File.open("data.csv", "w")&lt;br /&gt;	f &lt;&lt; "id,column1,column2,latitude,longitude"\n"&lt;br /&gt;	id = 1&lt;br /&gt;	nodes.each { |node|&lt;br /&gt;		column1 = node.elements["column1"].text&lt;br /&gt;		column2 = node.elements["column2"].text&lt;br /&gt;		coords = node.elements["Point"].elements["coordinates"].text.split(",")&lt;br /&gt;		f &lt;&lt; [id, column1, column2, coords[1], coords[0] ].join(",") &lt;&lt; "\n"&lt;br /&gt;		id += 1&lt;br /&gt;	}&lt;br /&gt;ensure&lt;br /&gt;	f.close&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 18:56:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1765</guid>
      <author>ajturner (Andrew Turner)</author>
    </item>
    <item>
      <title>US State Abbreviations to Full name</title>
      <link>http://snippets.dzone.com/posts/show/1764</link>
      <description># US State abbreviations to full name&lt;br /&gt;# state_name = state_abbr.[]("MI")&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;state_abbr = {&lt;br /&gt;  'AL' =&gt; 'Alabama',&lt;br /&gt;  'AK' =&gt; 'Alaska',&lt;br /&gt;  'AS' =&gt; 'America Samoa',&lt;br /&gt;  'AZ' =&gt; 'Arizona',&lt;br /&gt;  'AR' =&gt; 'Arkansas',&lt;br /&gt;  'CA' =&gt; 'California',&lt;br /&gt;  'CO' =&gt; 'Colorado',&lt;br /&gt;  'CT' =&gt; 'Connecticut',&lt;br /&gt;  'DE' =&gt; 'Delaware',&lt;br /&gt;  'DC' =&gt; 'District of Columbia',&lt;br /&gt;  'FM' =&gt; 'Micronesia1',&lt;br /&gt;  'FL' =&gt; 'Florida',&lt;br /&gt;  'GA' =&gt; 'Georgia',&lt;br /&gt;  'GU' =&gt; 'Guam',&lt;br /&gt;  'HI' =&gt; 'Hawaii',&lt;br /&gt;  'ID' =&gt; 'Idaho',&lt;br /&gt;  'IL' =&gt; 'Illinois',&lt;br /&gt;  'IN' =&gt; 'Indiana',&lt;br /&gt;  'IA' =&gt; 'Iowa',&lt;br /&gt;  'KS' =&gt; 'Kansas',&lt;br /&gt;  'KY' =&gt; 'Kentucky',&lt;br /&gt;  'LA' =&gt; 'Louisiana',&lt;br /&gt;  'ME' =&gt; 'Maine',&lt;br /&gt;  'MH' =&gt; 'Islands1',&lt;br /&gt;  'MD' =&gt; 'Maryland',&lt;br /&gt;  'MA' =&gt; 'Massachusetts',&lt;br /&gt;  'MI' =&gt; 'Michigan',&lt;br /&gt;  'MN' =&gt; 'Minnesota',&lt;br /&gt;  'MS' =&gt; 'Mississippi',&lt;br /&gt;  'MO' =&gt; 'Missouri',&lt;br /&gt;  'MT' =&gt; 'Montana',&lt;br /&gt;  'NE' =&gt; 'Nebraska',&lt;br /&gt;  'NV' =&gt; 'Nevada',&lt;br /&gt;  'NH' =&gt; 'New Hampshire',&lt;br /&gt;  'NJ' =&gt; 'New Jersey',&lt;br /&gt;  'NM' =&gt; 'New Mexico',&lt;br /&gt;  'NY' =&gt; 'New York',&lt;br /&gt;  'NC' =&gt; 'North Carolina',&lt;br /&gt;  'ND' =&gt; 'North Dakota',&lt;br /&gt;  'OH' =&gt; 'Ohio',&lt;br /&gt;  'OK' =&gt; 'Oklahoma',&lt;br /&gt;  'OR' =&gt; 'Oregon',&lt;br /&gt;  'PW' =&gt; 'Palau',&lt;br /&gt;  'PA' =&gt; 'Pennsylvania',&lt;br /&gt;  'PR' =&gt; 'Puerto Rico',&lt;br /&gt;  'RI' =&gt; 'Rhode Island',&lt;br /&gt;  'SC' =&gt; 'South Carolina',&lt;br /&gt;  'SD' =&gt; 'South Dakota',&lt;br /&gt;  'TN' =&gt; 'Tennessee',&lt;br /&gt;  'TX' =&gt; 'Texas',&lt;br /&gt;  'UT' =&gt; 'Utah',&lt;br /&gt;  'VT' =&gt; 'Vermont',&lt;br /&gt;  'VI' =&gt; 'Virgin Island',&lt;br /&gt;  'VA' =&gt; 'Virginia',&lt;br /&gt;  'WA' =&gt; 'Washington',&lt;br /&gt;  'WV' =&gt; 'West Virginia',&lt;br /&gt;  'WI' =&gt; 'Wisconsin',&lt;br /&gt;  'WY' =&gt; 'Wyoming'&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 18:47:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1764</guid>
      <author>ajturner (Andrew Turner)</author>
    </item>
    <item>
      <title>Average US State Locations</title>
      <link>http://snippets.dzone.com/posts/show/1763</link>
      <description># A hash table of average US State Locations (latitude, longitude)&lt;br /&gt;# MI_Location = state_locations.[]("MI")&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;state_locations = {&lt;br /&gt;  'AK' =&gt; [61.3850,-152.2683],&lt;br /&gt;  'AL' =&gt; [32.7990,-86.8073],&lt;br /&gt;  'AR' =&gt; [34.9513,-92.3809],&lt;br /&gt;  'AS' =&gt; [14.2417,-170.7197],&lt;br /&gt;  'AZ' =&gt; [33.7712,-111.3877],&lt;br /&gt;  'CA' =&gt; [36.1700,-119.7462],&lt;br /&gt;  'CO' =&gt; [39.0646,-105.3272],&lt;br /&gt;  'CT' =&gt; [41.5834,-72.7622],&lt;br /&gt;  'DC' =&gt; [38.8964,-77.0262],&lt;br /&gt;  'DE' =&gt; [39.3498,-75.5148],&lt;br /&gt;  'FL' =&gt; [27.8333,-81.7170],&lt;br /&gt;  'GA' =&gt; [32.9866,-83.6487],&lt;br /&gt;  'HI' =&gt; [21.1098,-157.5311],&lt;br /&gt;  'IA' =&gt; [42.0046,-93.2140],&lt;br /&gt;  'ID' =&gt; [44.2394,-114.5103],&lt;br /&gt;  'IL' =&gt; [40.3363,-89.0022],&lt;br /&gt;  'IN' =&gt; [39.8647,-86.2604],&lt;br /&gt;  'KS' =&gt; [38.5111,-96.8005],&lt;br /&gt;  'KY' =&gt; [37.6690,-84.6514],&lt;br /&gt;  'LA' =&gt; [31.1801,-91.8749],&lt;br /&gt;  'MA' =&gt; [42.2373,-71.5314],&lt;br /&gt;  'MD' =&gt; [39.0724,-76.7902],&lt;br /&gt;  'ME' =&gt; [44.6074,-69.3977],&lt;br /&gt;  'MI' =&gt; [43.3504,-84.5603],&lt;br /&gt;  'MN' =&gt; [45.7326,-93.9196],&lt;br /&gt;  'MO' =&gt; [38.4623,-92.3020],&lt;br /&gt;  'MP' =&gt; [14.8058,145.5505],&lt;br /&gt;  'MS' =&gt; [32.7673,-89.6812],&lt;br /&gt;  'MT' =&gt; [46.9048,-110.3261],&lt;br /&gt;  'NC' =&gt; [35.6411,-79.8431],&lt;br /&gt;  'ND' =&gt; [47.5362,-99.7930],&lt;br /&gt;  'NE' =&gt; [41.1289,-98.2883],&lt;br /&gt;  'NH' =&gt; [43.4108,-71.5653],&lt;br /&gt;  'NJ' =&gt; [40.3140,-74.5089],&lt;br /&gt;  'NM' =&gt; [34.8375,-106.2371],&lt;br /&gt;  'NV' =&gt; [38.4199,-117.1219],&lt;br /&gt;  'NY' =&gt; [42.1497,-74.9384],&lt;br /&gt;  'OH' =&gt; [40.3736,-82.7755],&lt;br /&gt;  'OK' =&gt; [35.5376,-96.9247],&lt;br /&gt;  'OR' =&gt; [44.5672,-122.1269],&lt;br /&gt;  'PA' =&gt; [40.5773,-77.2640],&lt;br /&gt;  'PR' =&gt; [18.2766,-66.3350],&lt;br /&gt;  'RI' =&gt; [41.6772,-71.5101],&lt;br /&gt;  'SC' =&gt; [33.8191,-80.9066],&lt;br /&gt;  'SD' =&gt; [44.2853,-99.4632],&lt;br /&gt;  'TN' =&gt; [35.7449,-86.7489],&lt;br /&gt;  'TX' =&gt; [31.1060,-97.6475],&lt;br /&gt;  'UT' =&gt; [40.1135,-111.8535],&lt;br /&gt;  'VA' =&gt; [37.7680,-78.2057],&lt;br /&gt;  'VI' =&gt; [18.0001,-64.8199],&lt;br /&gt;  'VT' =&gt; [44.0407,-72.7093],&lt;br /&gt;  'WA' =&gt; [47.3917,-121.5708],&lt;br /&gt;  'WI' =&gt; [44.2563,-89.6385],&lt;br /&gt;  'WV' =&gt; [38.4680,-80.9696],&lt;br /&gt;  'WY' =&gt; [42.7475,-107.2085]&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 18:44:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1763</guid>
      <author>ajturner (Andrew Turner)</author>
    </item>
    <item>
      <title>Mobile location information</title>
      <link>http://snippets.dzone.com/posts/show/272</link>
      <description>&lt;code&gt;&lt;br /&gt;import location&lt;br /&gt;mcc, mnc, lac, cellid = location.gsm_location()&lt;br /&gt;# lac, cellid can be used to guess your location&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;mcc = Mobile Country Code&lt;br /&gt;mnc = Mobile Network Code&lt;br /&gt;lac = Location Area Code&lt;br /&gt;cellid = Cell Id&lt;br /&gt;&lt;br /&gt;mcc and mnc is the same wherever you are.&lt;br /&gt;Normally, you would collect some data of your location&lt;br /&gt;and match it with lac/cellid. Then later you can guess&lt;br /&gt;your location from current lac/cellid.</description>
      <pubDate>Wed, 11 May 2005 16:54:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/272</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
