ruby 使用从osm.pbf输入中提取的多边形列表编辑输入osm文件的过程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 使用从osm.pbf输入中提取的多边形列表编辑输入osm文件的过程相关的知识,希望对你有一定的参考价值。

## using OSMDataManager Module for removing multiple polygon areas from OSM files
### desctiption and example

This module is created for altering Open Street Map's data, in order to remove multiple areas (chuncks) using .poly files.
Specifications regarding the `poly` format could be found [here](http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format). Example and tutorial for creating `poly` files in QGIS are available [here](https://mvexel.blog/2011/11/05/tutorial-poly/).

The _manager_ instance is initialized with two arguements: path to the folser containing the `poly` file(s) and open street map data, for example `m` is initiated in example below :


with two input variables where `/home/ubuntu/scripts/polyrema/` is the location where the `.poly` files are located, with a base OSM file downloaded from `geofabrik`'s data extracts webpage.


```{ruby}
m = OSMDataManager::OSMDataEvent.new('/home/ubuntu/scripts/polyrema/','/home/ubuntu/data/us-northeast-latest.osm.pbf')

```
Additionaly, it's a good idea to first clip the base OSM file to a smaller extent. This could be done by passing the location of another `.poly` file, so


```{ruby}
m.clipExtent("/home/ubuntu/scripts/bound/mapoly.poly")
```

will clip the northeast to a smaller area of only a state (here MA).

Running the following will start a chain of processes for extracting each polygon, creating a intermidiete polygon and repeating this for the next polygon:

```{r}
m.go
```
require 'mixlib/shellout'
require 'httparty'


module OSMDataManager



    def OSMDataManager.chainProcess(f, baseosm)
        puts "chain process"
        cmd = Mixlib::ShellOut.new("sudo osmconvert #{baseosm} -B=#{fOLNAME}#{f.sub('.poly','')}.poly --complex-ways --drop-brokenrefs | sudo osmconvert #{baseosm} --subtract - -o=#{fOLNAME}#{f.sub('.poly','')}_floodin-subtracted.pbf")
        cmd.run_command
        puts "#{cmd.stdout}"
        puts "#{cmd.stderr}"
        if cmd.error!
            return "#{baseosm}"
        else
            return "#{fOLNAME}#{f.sub('.poly','')}_floodin-subtracted.pbf"
        end
    end
    
    def OSMDataManager.findOSM(f,baseosm)
        if ( f =~ /.*.osm$/ )
            puts ".osm is at the end"
            cmd = Mixlib::ShellOut.new("sudo","osmconvert", baseosm, "--subtract","#{fOLNAME}#{f}","-o=#{fOLNAME}#{f.sub('.osm','')}-subtracted.osm")
            cmd.run_command
        end
    end

    def OSMDataManager.convBack f
        cmd = Mixlib::ShellOut.new("sudo","osmconvert","#{fOLNAME}#{f.sub('.osm','')}-subtracted.osm","-o=#{fOLNAME}floodin-subtracted.pbf")
        cmd.run_command
    end


    def OSMDataManager.removeOsm(f,fOLNAME)
        cmd = Mixlib::ShellOut.new("sudo","rm","#{fOLNAME}#{f.sub('.osm','')}-subtracted.osm")
        cmd.run_command
    end

    def OSMDataManager.getDir fOLNAME
        Dir.foreach fOLNAME
    end

    def OSMDataManager.processSub(en)
        en.each do |item|
            next if item == '.' or item == '..' or item =~ /.*.osm$/
            pname = item.sub('.poly','')
            puts pname    
            cmd = Mixlib::ShellOut.new("sudo","osmconvert", "/home/ubuntu/data/us-northeast-latest.pbf", "-B=#{fOLNAME}#{pname}.poly","--complex-ways","--drop-brokenrefs","-o=#{fOLNAME}flood_#{pname}.osm")
            cmd.run_command
            puts cmd.stdout
        end
        
    end



    class OSMDataEvent
        attr_reader :fOLNAME
        def initialize(inFoldername,baseosm)
            @baseosm = baseosm
            @delbase = 'pass'
            @fOLNAME = inFoldername
        end


        def appendBackExtent(baseosm,ext)
            cmd = Mixlib::ShellOut.new("sudo osmconvert #{baseosm} -B=#{ext} --complex-ways --drop-brokenrefs | sudo osmconvert #{baseosm} --subtract - -o=#{@fOLNAME}base-subtracted-rest.pbf")
            cmd.run_command
        end
        

        def clipExtent(ext)
            @ext = ext

            puts "cliping based on the input extent"
            cmd = Mixlib::ShellOut.new("sudo osmconvert #{@baseosm} -B=#{ext} --complex-ways --drop-brokenrefs -o=#{@fOLNAME}base-subtracted-extent.pbf")
            cmd.run_command
            if cmd.error!
                puts "couldnt subtract base based on oxtent"
            else
                @baseosm = "#{@fOLNAME}base-subtracted-extent.pbf"
                puts "subtracted base on extent now base is #{@baseosm}"
            end


        end



        def go
            e = OSMDataManager.getDir @fOLNAME
            e.each do |item|
                puts item
                next if item == '.' or item == '..'
                m.processF(item)
            end
        end

        def processF f
            puts "processing #{f}, @baseosm #{@baseosm}"
            if ( f =~ /.*\b.poly$/ )# && (f =~ /flood/)!=0
                begin
                    
                    chpou = OSMDataManager.chainProcess(f, @baseosm)
                    
                rescue => exception

                    puts " error in processing, will keep the base and pass"
                    @delbase = 'pass'

                else
                    if (@baseosm == "/home/ubuntu/data/us-northeast-latest.osm.pbf")
                        puts "it was the base northeast osm dont delete prev"
                        @baseosm = "#{chpou}"
                        @delbase = 'pass'
                    elsif (@baseosm == "base-subtracted-extent.pbf")
                        puts "it was the base extent dont delete prev"
                        @baseosm = "#{chpou}"
                        @delbase = 'pass'
                    else
                        
                        @delbase = "#{@baseosm}"
                        @baseosm = "#{chpou}"
                    end
                    
                ensure
                    puts "this is ensure"
                    if @delbase == 'pass'
                        puts "ensudere passes"
                    else 
                        puts "ensure deletes"
                        cmd = Mixlib::ShellOut.new("sudo rm #{@delbase}")
                        cmd.run_command
                    end
                                        
                end
                              
                puts "chain process returns #{chpou}"
                
            end
        end

    end



end

以上是关于ruby 使用从osm.pbf输入中提取的多边形列表编辑输入osm文件的过程的主要内容,如果未能解决你的问题,请参考以下文章

ruby 使用从osm.pbf输入中提取的多边形列表编辑输入osm文件的过程

osm-File(osm.pbf) 的处理和过滤在 C# 中花费的时间太长

osm pbf格式怎么转xml

将 shp 文件(sf 对象)转换为 osm pbf?

Protobuf-net 反序列化开放街道地图

使用Postgis使用SQL查询在Postgres中使用OSM给出(lat,long)时检索最近的道路