irasally avatar irasally committed e3d84b7

緯度と経度を正しく計算してKMLファイルを出力する

Comments (0)

Files changed (1)

+# -*- coding: utf-8 -*-
+class KmlCreator
+  require 'kml'
+  def create(dir_path)
+    kml = KMLFile.new
+    latitudes_list = []
+    Dir::glob(dir_path).sort.each do |name|
+      latitudes_list += latitudes_list(name)
+    end
+    document = create_document
+    latitudes_list.each_with_index do |latitudes, idx|
+      document.features << create_placemark(latitudes, idx)
+    end
+    kml.objects << document
+    File.new("/Users/sally/kml/kml_#{Time.now.strftime('%Y%m%d%H%M')}.kml", "w").write(kml.render)
+  end
+
+  private
+  def create_document
+    KML::Document.new(
+                      :name => 'Driving cource - GW 2012',
+                      :description => 'GWドライブ走行ルートの記録',
+                      :styles => [
+                                  KML::Style.new(
+                                                 :id => 'driving_root',
+                                                 :line_style => KML::LineStyle.new(:color => '6f9314ff', :width => 4), # color: Transparency, Blue, Green, Red
+                                                 )
+                                 ] )
+  end
+  def latitude(gps_log)
+    [gps_log.longitude, gps_log.latitude]
+  end
+  def latitudes_list(file_name)
+    latitudes_list = []
+    latitudes = []
+    File.open(file_name).each_with_index do |line, i|
+      gps_log = GpsLog.new(line)
+      next if gps_log.header == 'Tag'
+      # '-99'は電源が入った(再計測を開始するために位置情報を確認する)ところっぽい
+      if gps_log.header == '-99'
+        latitudes_list << latitudes unless latitudes.empty?
+        latitudes = []
+        next
+      end
+      latitudes << latitude(gps_log)
+    end
+    latitudes_list
+  end
+  def create_placemark(latitudes, idx)
+    KML::Placemark.new(
+                       :name => "Driving root 20120509-#{idx}",
+                       :description => 'driving_root',
+                       :style_url => '#driving_root',
+                       :geometry => KML::LineString.new(
+                                                        :extrude => true,
+                                                        :tessellate => false,
+                                                        :coordinates => latitudes
+                                                        )
+                       )
+  end
+end
+
+class GpsLog
+  # $GPGGA UTC(hhmmss.sss),Latitude,N/S,Longitude,E/W,Fix quality,Fix quality,Horizontal dilution of position,Altitude,Height of geoid,,Checksum
+  # $GPRMC UTC(hhmmss.sss),A,Latitude,N/S,Longitude,E/W,Speed(knots),Course(degrees),Date(ddmmyy),,Checksum
+  attr_accessor :format, :header, :latitude, :longitude
+  def initialize(line)
+    cells = line.split(',')
+    self.header = cells[0]
+    self.format = cells[1]
+    case self.format
+    when "$GPGGA"
+      set_latitude(cells[3], cells[4])
+      set_longitude(cells[5], cells[6])
+    when '$GPRMC'
+      set_latitude(cells[4], cells[5])
+      set_longitude(cells[6], cells[7])
+    end
+  end
+
+  private
+  def set_latitude(data, direction)
+    degree = data.to_i / 100 # 度
+    self.latitude = degree + (data.to_f - degree*100) / 60 # 分表示の部分を10進数で表す
+    self.latitude *= -1 if direction == 'S' # 南緯はマイナス値
+  end
+  def set_longitude(data, direction)
+    degree = data.to_i / 100 # 度
+    self.longitude = degree + (data.to_f - degree*100) / 60 # 分表示の部分を10進数で表す
+    self.longitude *= -1 if direction == 'W' # 西経はマイナス値
+  end
+end
+
+KmlCreator.new.create "/Users/sally/Downloads/0201205092144/*.gps"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.