GPS のログを OpenStreetMap に重ねて表示するページを作るスクリプト

GT-730FL-S MAPLUS

GPS のログを OpenStreetMap に重ねて表示する方法 Openlayers Track example に記述されている HTML ファイルを作成するスクリプトを作った。
なぜか cygwin 上のシェルスクリプト。
MAPLUS ポータブルナビ3の GPS ファイルとその他 gpx ファイルを置いた フォルダで実行すると 軌跡を OSM の地図に重ねて書く HTMLを書いてくれる。
縮尺の自動調整が課題

'10/11/23 エンコードの指定追加
'12/05/20 Osmarender 関連行をコメントアウト

TMPFILE=/tmp/$$

for GPS in *.GPS
do
  GPX=`echo $GPS | sed -e 's/.GPS/.gpx/'`
  echo $GPS '->' $GPX

  sed \
      -e '/^\$GPGGA,[^,]*,0/d' \
      -e '/^\$GPRMC,[^,]*,[^,]*,0/d' \
      -e '/^\$GPGLL,0/d' \
      $GPS > $TMPFILE

  /Program\ Files/GPSBabel/GPSBABEL -i nmea -f $TMPFILE -o gpx -F $GPX
  rm $TMPFILE
done

# lat lon
# <trkpt lat="34.931079100" lon="135.724934900">
LAT1MIN=90
LAT2MIN=9999
LON1MIN=180
LON2MIN=9999
LAT1MAX=0
LAT2MAX=0
LON1MAX=0
LON2MAX=0

set `sed -n -e 's/^.*lat="\([0-9][0-9]*\)\.\([0-9]\{1,4\}\).*" lon="\([0-9][0-9]*\)\.\([0-9]\{1,4\}\).*".*$/\1 \2 \3 \4/p' *.gpx |
while read LAT1 LAT2 LON1 LON2
do
  if [ $LAT1 -eq $LAT1MIN -a $LAT2 -lt $LAT2MIN -o \
       $LAT1 -lt $LAT1MIN ]
  then
      LAT1MIN=$LAT1
      LAT2MIN=$LAT2
  fi
  if [ $LAT1 -eq $LAT1MAX -a $LAT2 -gt $LAT2MAX -o \
       $LAT1 -gt $LAT1MAX ]
  then
      LAT1MAX=$LAT1
      LAT2MAX=$LAT2
  fi


  if [ $LON1 -eq $LON1MIN -a $LON2 -lt $LON2MIN -o \
       $LON1 -lt $LON1MIN ]
  then
      LON1MIN=$LON1
      LON2MIN=$LON2
  fi
  if [ $LON1 -eq $LON1MAX -a $LON2 -gt $LON2MAX -o \
       $LON1 -gt $LON1MAX ]
  then
      LON1MAX=$LON1
      LON2MAX=$LON2
  fi
  echo $LAT1MIN $LAT2MIN $LAT1MAX $LAT2MAX $LON1MIN $LON2MIN $LON1MAX $LON2MAX
done | tail -1`
LATMIN=$1$2
LATMAX=$3$4
LONMIN=$5$6
LONMAX=$7$8

echo $LATMIN $LONMIN $LATMAX $LONMAX

LAT=`expr \( $LATMIN + $LATMAX \) / 2 | sed -e 's/^\([0-9][0-9]\)\([0-9][0-9]\{1,4\}\).*$/\1.\2/'`
LON=`expr \( $LONMIN + $LONMAX \) / 2 | sed -e 's/^\([0-9][0-9][0-9]\)\([0-9]\{1,4\}[0-9]*\)$/\1.\2/'`

echo $LATMIN $LATMAX $LAT
echo $LONMIN $LONMAX $LON

cat > index.html <<EOF
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!--- add for firefox Nov.23rd, 2010 --->
	<title>Simple OSM GPX Track</title>
	<!-- bring in the OpenLayers javascript library
		 (here we bring it from the remote site, but you could
		 easily serve up this javascript yourself) -->
	<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
	<!-- bring in the OpenStreetMap OpenLayers layers.
		 Using this hosted file will make sure we are kept up
		 to date with any necessary changes -->
        <script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script>
 
	<script type="text/javascript">
		// Start position for the map (hardcoded here for simplicity,
		// but maybe you want to get from URL params)
		var lat=$LAT
		var lon=$LON
		var zoom=10
 
		var map; //complex object of type OpenLayers.Map
 
		function init() {
			map = new OpenLayers.Map ("map", {
				controls:[
					new OpenLayers.Control.Navigation(),
					new OpenLayers.Control.PanZoomBar(),
					new OpenLayers.Control.LayerSwitcher(),
					new OpenLayers.Control.Attribution()],
				maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
				maxResolution: 156543.0399,
				numZoomLevels: 19,
				units: 'm',
				projection: new OpenLayers.Projection("EPSG:900913"),
				displayProjection: new OpenLayers.Projection("EPSG:4326")
			} );
 
 
			// Define the map layer
			// Note that we use a predefined layer that will be
			// kept up to date with URL changes
			// Here we define just one layer, but providing a choice
			// of several layers is also quite simple
			// Other defined layers are OpenLayers.Layer.OSM.Mapnik, OpenLayers.Layer.OSM.Maplint and OpenLayers.Layer.OSM.CycleMap
			layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
			map.addLayer(layerMapnik);
// removed			layerTilesAtHome = new OpenLayers.Layer.OSM.Osmarender("Osmarender");
// removed			map.addLayer(layerTilesAtHome);
			layerCycleMap = new OpenLayers.Layer.OSM.CycleMap("CycleMap");
			map.addLayer(layerCycleMap);
			layerMarkers = new OpenLayers.Layer.Markers("Markers");
			map.addLayer(layerMarkers);
 

                        // Add the Layer with GPX Track

EOF
for gpx in *.gpx
do
   GPS=`echo $gpx | sed -e 's/.gpx/.GPS/'`
   if [ -f $GPS ]
   then
       cat >> index.html <<EOF
                        var lgpx = new OpenLayers.Layer.GML("MB Bruderholz", "$gpx", {
                            format: OpenLayers.Format.GPX,
                            style: {strokeColor: "blue", strokeWidth: 5, strokeOpacity: 0.5},
                            projection: new OpenLayers.Projection("EPSG:4326")
                        });
                        map.addLayer(lgpx);
EOF
  else
       cat >> index.html <<EOF
                        var lgpx = new OpenLayers.Layer.GML("MB Bruderholz", "$gpx", {
                            format: OpenLayers.Format.GPX,
                            style: {strokeColor: "red", strokeWidth: 5, strokeOpacity: 0.5},
                            projection: new OpenLayers.Projection("EPSG:4326")
                        });
                        map.addLayer(lgpx);
EOF
  fi
done

cat >> index.html <<EOF

			var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
			map.setCenter (lonLat, zoom);
 
		}
	</script>
 
</head>
<!-- body.onload is called once the page is loaded (call the 'init' function) -->
<body onload="init();">
	<!-- define a DIV into which the map will appear. Make it take up the whole window -->
	<div style="width:100%; height:100%" id="map"></div>
</body>
</html>
EOF




X01T から Touch Diamond に DUN
山下康成の
きばらないブログ

フリック入力化

ツイート Tweet to @yasunari_y @yasunari_yをフォロー

Copyright (C) 2003-2017 Yasunari Yamashita. All Rights Reserved.
yasunari @ yamasita.jp 山下康成@京都府向日市