param ( [Parameter(Mandatory=$true)][string]$osmFile, [Parameter(Mandatory=$true)][string]$minLatitude , [Parameter(Mandatory=$true)][string]$minLongitude , [Parameter(Mandatory=$true)][string]$maxLatitude, [Parameter(Mandatory=$true)][string]$maxLongitude ) function RemoveContourGlitches () { ForEach ($Way in $osmContent.osm.way) { $nodesinWay = $way.SelectNodes("./nd") $count = $nodesinWay.Count if ($count -lt 10) { RemoveReferencedNodes $Way.ParentNode.RemoveChild($Way) # remove this way } } } function RemoveWay ([string]$elevationValueToRemove) { ForEach($Tag in $osmContent.osm.way.tag) { $key=$Tag.GetAttribute("k") $value=$Tag.GetAttribute("v") if($key.Equals("ele") -and $value.Equals($elevationValueToRemove)) { $Way = $Tag.ParentNode Write-Host "removed way " $Way.id " with elevation = " $elevationValueToRemove RemoveReferencedNodes $Way.ParentNode.RemoveChild($Way) # remove this way } } } function RemoveReferencedNodes { $nd = $Way.FirstChild # for each node referenced in this way while (!($nd -eq $null )) { $ref = $nd.GetAttribute("ref") $node=($osmContent.osm.node|where{$_.id -eq "$ref"}) if (!($node -eq $null)) { $Node.ParentNode.RemoveChild($Node) } $nd = $nd.get_nextsibling() } } function AddZoneBoundaryTag ( [string]$elevation, [string]$newKey, [string]$newValue) { ForEach($Tag in $osmContent.osm.way.tag) { $key =$Tag.GetAttribute("k") $value=$Tag.GetAttribute("v") if($key.Equals("ele") -and $value.Equals($elevation)) { $AddTag = $osmContent.CreateElement("tag") $newKeyAttr = $osmContent.CreateAttribute("k") $newKeyAttr.Value = "$newKey" $AddTag.Attributes.Append($newKeyAttr) $newValueAttr = $osmContent.CreateAttribute("v") $newValueAttr.Value = "$newValue" $AddTag.Attributes.Append($newValueAttr) $Way = $Tag.ParentNode $Way.AppendChild($AddTag) } } } function AddBoundaryBox () { $newWay = @($osmContent.osm.way)[-1].Clone() $newWay.id = ([int64]($newWay.id) - 1).ToString() $todelete = $newWay.FirstChild; while (!($todelete -eq $null)) { $newWay.RemoveChild($toDelete); $todelete = $newWay.FirstChild; } $newNode = @($osmContent.osm.node)[-1].Clone() AddNode $MinLongitude $MinLatitude AddNd $newNode = @($osmContent.osm.node)[-1].Clone() AddNode $MaxLongitude $MinLatitude AddNd $newNode = @($osmContent.osm.node)[-1].Clone() AddNode $MaxLongitude $MaxLatitude AddNd $newNode = @($osmContent.osm.node)[-1].Clone() AddNode $MinLongitude $MaxLatitude AddNd $newNode = @($osmContent.osm.node)[-1].Clone() AddNode $MinLongitude $MinLatitude AddNd $newTag = $osmContent.CreateElement("tag") $newKeyAttr = $osmContent.CreateAttribute("k") $newKeyAttr.Value = "boundary" $newTag.Attributes.Append($newKeyAttr) $newValueAttr = $osmContent.CreateAttribute("v") $newValueAttr.Value = "box" $newTag.Attributes.Append($newValueAttr) $newWay.AppendChild($newTag) $osmContent.osm.AppendChild($newWay) } function AddNode ([string] $lon, [string] $lat) { $newNode.id = ([int64]($newNode.id) - 1).ToString() $newNode.lat=$lat $newNode.lon=$lon $osmcontent.osm.AppendChild($newNode) } function AddNd() { $newNd = @($osmContent.osm.way.nd)[-1].Clone() $id = $newNode.id $newNd.ref="$id" $newWay.AppendChild($newNd) } #----------------------------------------------------------------------- # main #----------------------------------------------------------------------- #Get-ChildItem Env: $srtmCache = $Env:srtm2osmRoot + "\SrtmCache" $bounds = "-bounds1 " + "$minLatitude" + " " + "$minLongitude" + " " + "$maxLatitude" + " " + "$maxLongitude" $arguments = $bounds + " -step 500 -large -o " + $osmFile + " -d " + $srtmCache $run_srtm2osm = $Env:srtm2osmRoot + "\srtm2osm\Srtm2OSM.exe " Start-Process -FilePath "$run_srtm2osm" -ArgumentList "$arguments" -Wait -PassThru [xml]$osmContent = Get-Content $osmFile -Encoding UTF8 RemoveWay "0" AddZoneBoundaryTag "500" "natural" "peak" AddZoneBoundaryTag "500" "height" "MidFoothills" AddZoneBoundaryTag "1000" "natural" "peak" AddZoneBoundaryTag "1000" "height" "LowerMontane" AddZoneBoundaryTag "1500" "natural" "peak" AddZoneBoundaryTag "1500" "height" "MidMontane" AddZoneBoundaryTag "2000" "natural" "peak" AddZoneBoundaryTag "2000" "height" "SubAlpine" RemoveWay "2500" RemoveWay "3000" RemoveWay "3500" RemoveWay "4000" RemoveContourGlitches AddBoundaryBox $osmContent.Save($osmFile)