0

为了阅读 OpenStreetMap 数据,我使用了一个名为“OsmSharp”的库,没有太多的文档,但足以让我四处走动。

目前我被卡住了,想知道是否可以让两个线程从 PBFOsmStreamSource 读取数据,然后同时对其进行过滤。

这是一个坏习惯吗?它甚至做点什么吗?

using OsmSharp;
using OsmSharp.Streams;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp
{
    public class SomeClass
    {
        public async Task GetSomeData()
        {
            using var fileStream = File.OpenRead("some-data.osm.pbf");
            var source = new PBFOsmStreamSource(fileStream);

            var filterFirst = Task.Run(() => FilterFirst(source));
            var filterSecond = Task.Run(() => FilterSecond(source));

            // Do some other stuff

            var resultFirst = await filterFirst;
            var resultSecond = await filterFirst;

            // Do stuff with filtered data
        }

        private IEnumerable<OsmGeo> FilterSecond(PBFOsmStreamSource source)
        {
            var filtered = from osmGeo in source
                           where osmGeo.Type == OsmGeoType.Way
                           && osmGeo.Tags != null
                           // Some more filtering
                           select osmGeo;

            return filtered;
        }

        private IEnumerable<OsmGeo> FilterFirst(PBFOsmStreamSource source)
        {
            var filtered = from osmGeo in source
                           where osmGeo.Type == OsmGeoType.Node
                               && osmGeo.Tags != null
                               // Some more filtering
                           select osmGeo;

            return filtered;
        }
    }
}
4

0 回答 0