Пример #1
0
        public IEnumerable <OsmRelation> ReadAllRelations()
        {
            var blobDecoder = BlobSmtDecoder(relationIndex, (blob, buf) =>
            {
                try
                {
                    OsmRelation[] tmp;
                    int len = PbfFast.DecodePrimitiveBlock(buf, 0, blob, out tmp);
                    if (len != blob.rawSize)
                    {
                        throw new PbfParseException();
                    }
                    return(tmp);
                }
                catch
                {
                    return(null);
                }
            });

            foreach (var blob in blobDecoder)
            {
                foreach (var rel in blob)
                {
                    yield return(rel);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// liest alle Knoten aus
        /// </summary>
        /// <returns>Enumerable aller Knoten</returns>
        public IEnumerable <OsmNode> ReadAllNodes()
        {
            var blobDecoder = BlobSmtDecoder(nodeIndex, (blob, buf) =>
            {
                try
                {
                    OsmNode[] tmp;
                    int len = PbfFast.DecodePrimitiveBlock(buf, 0, blob, out tmp);
                    if (len != blob.rawSize)
                    {
                        throw new PbfParseException();
                    }
                    return(tmp);
                }
                catch
                {
                    return(null);
                }
            }).GetEnumerator();

            OsmNode[] nodes    = null;
            var       tmpNodes = new List <OsmNode[]>();

            foreach (var nodeBlob in nodeIndex)
            {
                if (nodes == null || nodes[0].id != nodeBlob.minNodeId)
                {
                    nodes = null;
                    for (int i = 0; i < tmpNodes.Count; i++)
                    {
                        if (tmpNodes[i][0].id == nodeBlob.minNodeId)
                        {
                            nodes = tmpNodes[i];
                            tmpNodes.RemoveAt(i);
                            break;
                        }
                    }
                    while (nodes == null)
                    {
                        blobDecoder.MoveNext();
                        nodes = blobDecoder.Current;
                        if (nodes[0].id != nodeBlob.minNodeId)
                        {
                            tmpNodes.Add(nodes);
                            nodes = null;
                        }
                    }
                }

                foreach (var node in nodes)
                {
                    yield return(node);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="planetFilePbf">PBF-Datei, welche geöffnet werden soll (z.B. planet-latest.osm.pbf)</param>
        public OsmPbfReader(string planetFilePbf)
        {
            var index = PbfFast.ReadIndex(planetFilePbf, false);

            nodeIndex     = index.Where(x => x.nodeCount > 0).ToArray();
            wayIndex      = index.Where(x => x.wayCount > 0).ToArray();
            relationIndex = index.Where(x => x.relationCount > 0).ToArray();
            pbfReader     = new FastPbfReader(planetFilePbf)
            {
                RandomBuffering = true
            };
        }
Пример #4
0
 /// <summary>
 /// liest alle Knoten aus
 /// </summary>
 /// <returns>Enumerable aller Knoten</returns>
 public IEnumerable <MemArray <OsmNode> > ReadAllNodes4()
 {
     foreach (var blob in BlobSmtDecoder(nodeIndex, (blob, buf) =>
     {
         MemArray <OsmNode> tmp;
         int len = PbfFast.DecodePrimitiveBlock(buf, 0, blob, out tmp);
         if (len != blob.rawSize)
         {
             throw new PbfParseException();
         }
         return(tmp);
     }))
     {
         yield return(blob);
     }
 }
Пример #5
0
        /// <summary>
        /// liest ein oder mehrere OSM-Relationen ein und gibt die Ergebnisse in entsprechender Reihenfolge zurück
        /// </summary>
        /// <param name="relationIds">Relations-IDs, welche abgefragt werden sollen</param>
        /// <returns>abgefragte Relationen</returns>
        public OsmRelation[] ReadRelations(params long[] relationIds)
        {
            var result = new OsmRelation[relationIds.Length];

            var searchRelations = Enumerable.Range(0, relationIds.Length).Select(i => new KeyValuePair <long, int>(relationIds[i], i)).ToArray(relationIds.Length);

            Array.Sort(searchRelations, (x, y) => x.Key.CompareTo(y.Key));

            var needBlobs = new List <OsmBlob>();
            var lastBlob  = nodeIndex[0];

            foreach (var relationId in searchRelations.Select(x => x.Key))
            {
                if (relationId > lastBlob.maxRelationId || relationId < lastBlob.minRelationId)
                {
                    lastBlob = relationIndex.BinarySearchSingle(x => relationId >= x.minRelationId && relationId <= x.maxRelationId ? 0L : x.minRelationId - relationId);
                    needBlobs.Add(lastBlob);
                }
            }

            var blobDecoder = BlobSmtDecoder(needBlobs, (blob, buf) =>
            {
                try
                {
                    OsmRelation[] tmp;
                    int len = PbfFast.DecodePrimitiveBlock(buf, 0, blob, out tmp);
                    if (len != blob.rawSize)
                    {
                        throw new PbfParseException();
                    }
                    return(tmp);
                }
                catch
                {
                    return(null);
                }
            }).GetEnumerator();

            OsmRelation[] relations    = null;
            var           tmpRelations = new List <OsmRelation[]>();

            for (int r = 0; r < searchRelations.Length; r++)
            {
                long relationId = searchRelations[r].Key;

                if (relations == null || relations[relations.Length - 1].id < relationId)
                {
                    relations = null;
                    for (int i = 0; i < tmpRelations.Count; i++)
                    {
                        if (relationId >= tmpRelations[i].First().id&& relationId <= tmpRelations[i].Last().id)
                        {
                            relations = tmpRelations[i];
                            tmpRelations.RemoveAt(i);
                            break;
                        }
                    }
                    while (relations == null)
                    {
                        blobDecoder.MoveNext();
                        relations = blobDecoder.Current;
                        if (relationId < relations.First().id || relationId > relations.Last().id)
                        {
                            tmpRelations.Add(relations);
                            relations = null;
                        }
                    }

                    Console.WriteLine("read relations: {0:N0} / {1:N0}", r + 1, searchRelations.Length);
                }

                result[searchRelations[r].Value] = relations.BinarySearchSingle(x => x.id - relationId);
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// liest mehrere OSM-Knoten ein und gibt die Ergebnisse in entsprechender Reihenfolge zurück
        /// </summary>
        /// <param name="nodeIds">Knoten-IDs, welche abgefragt werden sollen</param>
        /// <returns>Array mit den abgefragten Knoten</returns>
        public OsmNode[] ReadNodes(params long[] nodeIds)
        {
            var result = new OsmNode[nodeIds.Length];

            var searchNodes = Enumerable.Range(0, nodeIds.Length).Select(i => new KeyValuePair <long, int>(nodeIds[i], i)).ToArray(nodeIds.Length);

            Array.Sort(searchNodes, (x, y) => x.Key.CompareTo(y.Key));

            var needBlobs = new List <OsmBlob>();
            var lastBlob  = wayIndex[0];

            foreach (var nodeId in searchNodes.Select(x => x.Key))
            {
                if (nodeId > lastBlob.maxNodeId || nodeId < lastBlob.minNodeId)
                {
                    lastBlob = nodeIndex.BinarySearchSingle(x => nodeId >= x.minNodeId && nodeId <= x.maxNodeId ? 0L : x.minNodeId - nodeId);
                    needBlobs.Add(lastBlob);
                }
            }

            var blobDecoder = BlobSmtDecoder(needBlobs, (blob, buf) =>
            {
                try
                {
                    MemArray <OsmNode> tmp;
                    int len = PbfFast.DecodePrimitiveBlock(buf, 0, blob, out tmp);
                    if (len != blob.rawSize)
                    {
                        throw new PbfParseException();
                    }
                    return(tmp);
                }
                catch
                {
                    return(null);
                }
            }).GetEnumerator();

            MemArray <OsmNode> nodes = null;
            var tmpNodes             = new List <MemArray <OsmNode> >();

            for (int w = 0; w < searchNodes.Length; w++)
            {
                long nodeId = searchNodes[w].Key;

                if (nodes == null || nodes[nodes.Length - 1].id < nodeId)
                {
                    if (nodes != null)
                    {
                        nodes.Dispose();
                    }
                    nodes = null;
                    for (int i = 0; i < tmpNodes.Count; i++)
                    {
                        if (nodeId >= tmpNodes[i].First().id&& nodeId <= tmpNodes[i].Last().id)
                        {
                            nodes = tmpNodes[i];
                            tmpNodes.RemoveAt(i);
                            break;
                        }
                    }
                    while (nodes == null)
                    {
                        blobDecoder.MoveNext();
                        nodes = blobDecoder.Current;
                        if (nodeId < nodes.First().id || nodeId > nodes.Last().id)
                        {
                            tmpNodes.Add(nodes);
                            nodes = null;
                        }
                    }

                    Console.WriteLine("read nodes: {0:N0} / {1:N0}", w + 1, searchNodes.Length);
                }

                result[searchNodes[w].Value] = nodes.BinarySearchSingle(x => x.id - nodeId);
            }
            if (nodes != null)
            {
                nodes.Dispose();
            }

            return(result);
        }