protected override Task DoTaskWork(string osmFile, AttributeRegistry attributeRegistry) { ExecuteSqlCmd("TRUNCATE TABLE [Node]"); ExecuteSqlCmd("TRUNCATE TABLE [NodeTag]"); var loadingNodeTable = new DataTable(); loadingNodeTable.TableName = "Node"; loadingNodeTable.MinimumCapacity = MaxRowCountInMemory; loadingNodeTable.Columns.Add("NodeId", typeof(long)); loadingNodeTable.Columns.Add("location", typeof(SqlGeography)); loadingNodeTable.Columns.Add("Latitude", typeof(double)); loadingNodeTable.Columns.Add("Longitude", typeof(double)); var dNodeTags = new DataTable(); dNodeTags.TableName = "NodeTag"; dNodeTags.MinimumCapacity = MaxRowCountInMemory; dNodeTags.Columns.Add("NodeId", typeof(long)); dNodeTags.Columns.Add("Typ"); dNodeTags.Columns.Add("Info"); IOsmReader reader = osmFile.EndsWith(".pbf") ? (IOsmReader) new PbfOsmReader() : (IOsmReader) new XmlOsmReader(); var insertingTask = Task.Factory.StartNew(() => StartInserting()); foreach (var node in reader.ReadNodes(osmFile, attributeRegistry)) { _countOfInsertedNodes++; loadingNodeTable = AddToCollection(loadingNodeTable, node.NodeId, node.ToSqlGeographyPoint(), node.Latitude, node.Longitude); foreach (var tag in node.Tags) { dNodeTags = AddToCollection(dNodeTags, node.NodeId, tag.Typ, tag.Value); } } DataTableCollection.Add(loadingNodeTable); DataTableCollection.Add(dNodeTags); DataTableCollection.CompleteAdding(); Trace.WriteLine(string.Format("Inserted {0} nodes", _countOfInsertedNodes)); return(insertingTask.Result); }
/// <summary> /// Creates a new instaance of the OsmDatabase class and loads data from specific IOsmReader into it. /// </summary> /// <param name="reader">The IOsmReader to read data from.</param> /// <returns>New instance of the OsmDAtabase class with data loaded from specified reader.</returns> public static OsmEntityInfoDatabase Load(IOsmReader reader) { OsmEntityInfoDatabase db = new OsmEntityInfoDatabase(); IEntityInfo entityInfo = null; while ((entityInfo = reader.Read()) != null) { switch (entityInfo.EntityType) { case EntityType.Node: db.Nodes.Add((NodeInfo)entityInfo); break; case EntityType.Way: db.Ways.Add((WayInfo)entityInfo); break; case EntityType.Relation: db.Relations.Add((RelationInfo)entityInfo); break; } } return(db); }
private void TestReader(IOsmReader reader) { IEntityInfo info = null; int nodesCount = 0, waysCount = 0, relationsCount = 0; while ((info = reader.Read()) != null) { switch (info.EntityType) { case EntityType.Node: nodesCount++; break; case EntityType.Way: waysCount++; break; case EntityType.Relation: relationsCount++; break; } } Assert.Equal(TestFileNodesCount, nodesCount); Assert.Equal(TestFileWaysCount, waysCount); Assert.Equal(TestFileRelationsCount, relationsCount); }
/// <summary> /// Creates a new instaance of the OsmDatabase class and loads data from specific IOsmReader into it. /// </summary> /// <param name="reader">The IOsmReader to read data from.</param> /// <param name="ignoreReferentialErrors">A value indicatings whether Load function should skip geometries that reference miising geometries.</param> /// <returns>New instance of the OsmDatabase class with data loaded from specified reader.</returns> public static OsmGeometryDatabase Load(IOsmReader reader, bool ignoreReferentialErrors) { OsmGeometryDatabase db = new OsmGeometryDatabase(); List <RelationInfo> relations = new List <RelationInfo>(); IEntityInfo entityInfo = null; while ((entityInfo = reader.Read()) != null) { switch (entityInfo.EntityType) { case EntityType.Node: db.Nodes.Add(Node.FromNodeInfo(entityInfo as NodeInfo)); break; case EntityType.Way: Way toAdd = Way.FromWayInfo(entityInfo as WayInfo, db, !ignoreReferentialErrors); if (toAdd == null) { if (!ignoreReferentialErrors) { throw new ArgumentException(string.Format("Way (ID = {0}) references missing node.", entityInfo.ID)); } } else { db.Ways.Add(toAdd); } break; case EntityType.Relation: RelationInfo ri = entityInfo as RelationInfo; db.Relations.Add(new Relation(ri.ID) { Tags = ri.Tags, Metadata = ri.Metadata }); relations.Add(ri); break; } } foreach (var relationInfo in relations) { Relation relation = db.Relations[relationInfo.ID]; foreach (var memberInfo in relationInfo.Members) { RelationMember member = RelationMember.FromRelationMemberInfo(memberInfo, db, false); if (member == null) { if (!ignoreReferentialErrors) { throw new ArgumentException(string.Format("Relation (ID = {0}) references missing OSM entity.", memberInfo.Reference)); } db.Relations.Remove(relation); } else { relation.Geometries.Add(member); } } } return(db); }