Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="file">
  <label>RWGのGPX: <input type="file" accept=".gpx"></label>
</div>
<div class="setting">
  <label>出力ポイント数: <input type="number" value="0"></label>
  <label>出力するファイル名: <input type="text" value="" class="fileName"></label>
  <label>出力するトラック名: <input type="text" value="" class="truckName"></label>
  <button>出力する</button>
</div>
<div class="download"></div>

              
            
!

CSS

              
                label {
  display: block;
}

.setting {
  display: none;
  margin: 1em 0 0;
  border-top: solid #ccc 1px;
  padding: 1em 0 0;
}

.download {
  display: none;
  margin: 1em 0 0;
  border-top: solid #ccc 1px;
  padding: 1em 0 0;
}
              
            
!

JS

              
                const setting = document.querySelector('.setting');
const download = document.querySelector('.download');
const inputFile = document.querySelector('input[type=file]');
const inputPoints = document.querySelector('input[type=number]');
const inputFileName = document.querySelector('.fileName');
const inputTruckName = document.querySelector('.truckName');
const button = document.querySelector('button');

//「ファイルを選択」でローカルファイルが設定された際の処理
const setFile = (event) => {
  //エリア表示のリセット
  setting.setAttribute('style','display: none;');
  download.setAttribute('style','display: none;');
  
  //ファイル名の取得と仮設定
  const fileObject = event.target.files[0];
  const temporaryFullName = fileObject.name;
  const temporaryName = temporaryFullName.split('.')[0];
  inputFileName.value = temporaryName + '_converted';

  const rwgGpx = new FileReader();
  rwgGpx.addEventListener('load', (event) => loadedFile(event));
  rwgGpx.readAsText(fileObject);
}
inputFile.addEventListener('change', (event) => setFile(event));

//ファイルの読み込みが完了した際の処理
const loadedFile = (event) => {
  const loadedGpx = event.target.result;

  //DOM準備
  const parser = new DOMParser();
  const temporaryGpx = parser.parseFromString(loadedGpx, 'application/xml');

  //前処理
  const baseGpx = temporaryGpx.documentElement;
  const baseTrk = baseGpx.querySelector('trk');
  const baseTrkName = baseTrk.querySelector('name');
  const baseTrkseg = baseTrk.querySelector('trkseg');
  const baseTrkpt = baseTrkseg.querySelectorAll('trkpt');

  //トラック名の取得と仮設定
  inputTruckName.value = baseTrkName.textContent;
  
  //ポイント数の取得と仮設定
  inputPoints.value = baseTrkpt.length;
  if(inputPoints.value > 9900) {
    inputPoints.value = 9900;
  }
  
  //Oregonには不要なデータを削除
  baseGpx.querySelector('metadata').remove();

  //設定用のエリアを表示(&ダウンロード用のエリアの非表示)
  setting.setAttribute('style','display: block;');

  //「出力する」ボタンのトリガー
  button.addEventListener('click', (event) => generateNewFile(event, temporaryGpx));
}

//ファイルを出力する際の処理
const generateNewFile = (event, temporaryGpx) => {
  //前処理
  const baseGpx = temporaryGpx.documentElement;
  const baseTrk = baseGpx.querySelector('trk');
  const baseTrkName = baseTrk.querySelector('name');
  const baseTrkseg = baseTrk.querySelector('trkseg');
  const baseTrkpt = baseTrkseg.querySelectorAll('trkpt');
  
  //ポイントの間引き
  if(baseTrkpt.length != inputPoints.value) {
    const temporaryTrkseg = document.createElement('trkseg');
    const thinningParameter = Math.ceil(baseTrkpt.length / inputPoints.value);
    baseTrkpt.forEach((element, index) => {
      const addElement = (element) => {
        const temporaryElevation = element.querySelector('ele');
        temporaryElevation.textContent = Math.round(temporaryElevation.textContent * 100) / 100;
        temporaryTrkseg.appendChild(element);
      }

      if(index % thinningParameter === 0) {
        addElement(element);
      } else if(index === baseTrkpt.length - 1) {
        addElement(element);
      }
    });
    
    //ポイントの入れ替え
    baseTrkseg.remove();
    baseTrk.appendChild(temporaryTrkseg);
  }
  
  //トラックネームの設定
  baseTrkName.textContent = inputTruckName.value;
  
  //出力用GPX用意
  const outputGpx = new XMLSerializer();
  let outputGpxText = outputGpx.serializeToString(temporaryGpx);
  outputGpxText = outputGpxText.replace(/<trkpt xmlns=\"http:\/\/www.topografix.com\/GPX\/1\/1\" lat=\"/g, '<trkpt lat="');
  outputGpxText = outputGpxText.replace(/<trkseg xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">/g, '<trkseg>');
  outputGpxText = outputGpxText.replace(/ {2,}/g, '');
  outputGpxText = outputGpxText.replace(/\r?\n/g, '');

  const blob = new Blob([outputGpxText], {type: 'application/xml'});

  //ダウンロード用のエリア内に要素がある場合は削除
  while(download.firstElementChild) {
    download.firstElementChild.remove();
  }

  //ファイルをダウンロードさせる
  const a = document.createElement('a');
  a.href = window.URL.createObjectURL(blob);
  a.textContent = 'ダウンロード';
  a.download = inputFileName.value + '.gpx';

  download.appendChild(a);

  //ダウンロード用のエリアを表示
  download.setAttribute('style','display: block;');
}

              
            
!
999px

Console