/// <summary> /// Encodes an OsmSharp-node into a PBF-node. /// </summary> public static OsmSharp.IO.PBF.Node EncodeNode(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.IO.PBF.Node pbfNode, OsmSharp.Node node) { pbfNode.id = node.Id.Value; pbfNode.info = new Info(); pbfNode.info.version = 0; if (node.ChangeSetId.HasValue) { pbfNode.info.changeset = node.ChangeSetId.Value; } else { pbfNode.info.changeset = 0; } if (node.TimeStamp.HasValue) { pbfNode.info.timestamp = Encoder.EncodeTimestamp(node.TimeStamp.Value, block.date_granularity); } else { pbfNode.info.timestamp = 0; } if (node.UserId.HasValue) { pbfNode.info.uid = (int)node.UserId.Value; } else { pbfNode.info.uid = 0; } pbfNode.info.user_sid = Encoder.EncodeString(block, reverseStringTable, node.UserName); if (node.Version.HasValue) { pbfNode.info.version = (int)node.Version.Value; } else { pbfNode.info.version = 0; } pbfNode.lat = Encoder.EncodeLatLon(node.Latitude.Value, block.lat_offset, block.granularity); pbfNode.lon = Encoder.EncodeLatLon(node.Longitude.Value, block.lon_offset, block.granularity); if (node.Tags != null) { foreach (var tag in node.Tags) { pbfNode.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key)); pbfNode.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value)); } } else { pbfNode.keys.Clear(); pbfNode.vals.Clear(); } return(pbfNode); }
/// <summary> /// Encodes an OsmSharp-way into a PBF-way. /// </summary> public static OsmSharp.IO.PBF.Way EncodeWay(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.Way way) { var pbfWay = new OsmSharp.IO.PBF.Way(); pbfWay.id = way.Id.Value; pbfWay.info = new Info(); if (way.ChangeSetId.HasValue) { pbfWay.info.changeset = way.ChangeSetId.Value; } if (way.TimeStamp.HasValue) { pbfWay.info.timestamp = Encoder.EncodeTimestamp(way.TimeStamp.Value, block.date_granularity); } if (way.UserId.HasValue) { pbfWay.info.uid = (int)way.UserId.Value; } pbfWay.info.user_sid = Encoder.EncodeString(block, reverseStringTable, way.UserName); pbfWay.info.version = 0; if (way.Version.HasValue) { pbfWay.info.version = (int)way.Version.Value; } if (way.Tags != null) { foreach (var tag in way.Tags) { pbfWay.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key)); pbfWay.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value)); } } if (way.Nodes != null && way.Nodes.Length > 0) { pbfWay.refs.Add(way.Nodes[0]); for (var i = 1; i < way.Nodes.Length; i++) { pbfWay.refs.Add(way.Nodes[i] - way.Nodes[i - 1]); } } return(pbfWay); }
private static void EncodeDenseNode(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, DenseNodes groupDense, OsmSharp.Node current, OsmSharp.Node previous) { groupDense.id.Add(current.Id.Value - previous.Id.Value); var currentLat = Encoder.EncodeLatLon(current.Latitude.Value, block.lat_offset, block.granularity); var currentLon = Encoder.EncodeLatLon(current.Longitude.Value, block.lon_offset, block.granularity); var previousLat = Encoder.EncodeLatLon(previous.Latitude.Value, block.lat_offset, block.granularity); var previousLon = Encoder.EncodeLatLon(previous.Longitude.Value, block.lon_offset, block.granularity); // Note that the offsets are actually cancelled out by the subtraction below // LatDiff equals (current.Latitude - previous.Latitude) * 100000000 / granularity var latDiff = currentLat - previousLat; var lonDiff = currentLon - previousLon; groupDense.lat.Add(latDiff); groupDense.lon.Add(lonDiff); if (current.Tags != null) { foreach (var nodeTag in current.Tags) { groupDense.keys_vals.Add(Encoder.EncodeString(block, reverseStringTable, nodeTag.Key)); groupDense.keys_vals.Add(Encoder.EncodeString(block, reverseStringTable, nodeTag.Value)); } groupDense.keys_vals.Add(0); } if (groupDense.denseinfo != null) { groupDense.denseinfo.changeset.Add(current.ChangeSetId.Value - previous.ChangeSetId.Value); var currentTimeStamp = Encoder.EncodeTimestamp(current.TimeStamp.Value, block.date_granularity); var previousTimeStamp = previous.TimeStamp == null ? 0 : Encoder.EncodeTimestamp(previous.TimeStamp.Value, block.date_granularity); groupDense.denseinfo.timestamp.Add(currentTimeStamp - previousTimeStamp); groupDense.denseinfo.uid.Add((int)(current.UserId.Value - previous.UserId.Value)); groupDense.denseinfo.version.Add((int)(current.Version.Value - previous.Version.Value)); var previousUserNameId = previous.UserName == null ? 0 : Encoder.EncodeString(block, reverseStringTable, previous.UserName); var currentUserNameId = Encoder.EncodeString(block, reverseStringTable, current.UserName); groupDense.denseinfo.user_sid.Add(currentUserNameId - previousUserNameId); } }
/// <summary> /// Encodes an OsmSharp-relation into a PBF-relation. /// </summary> /// <returns></returns> public static OsmSharp.IO.PBF.Relation EncodeRelation(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.Relation relation) { var pbfRelation = new OsmSharp.IO.PBF.Relation(); pbfRelation.id = relation.Id.Value; pbfRelation.info = new Info(); if (relation.ChangeSetId.HasValue) { pbfRelation.info.changeset = relation.ChangeSetId.Value; } if (relation.TimeStamp.HasValue) { pbfRelation.info.timestamp = Encoder.EncodeTimestamp(relation.TimeStamp.Value, block.date_granularity); } if (relation.UserId.HasValue) { pbfRelation.info.uid = (int)relation.UserId.Value; } pbfRelation.info.user_sid = Encoder.EncodeString(block, reverseStringTable, relation.UserName); pbfRelation.info.version = 0; if (relation.Version.HasValue) { pbfRelation.info.version = (int)relation.Version.Value; } if (relation.Tags != null) { foreach (var tag in relation.Tags) { pbfRelation.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key)); pbfRelation.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value)); } } if (relation.Members != null && relation.Members.Length > 0) { pbfRelation.memids.Add(relation.Members[0].Id); pbfRelation.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[0].Role)); switch (relation.Members[0].Type) { case OsmGeoType.Node: pbfRelation.types.Add(Relation.MemberType.NODE); break; case OsmGeoType.Way: pbfRelation.types.Add(Relation.MemberType.WAY); break; case OsmGeoType.Relation: pbfRelation.types.Add(Relation.MemberType.RELATION); break; } for (var i = 1; i < relation.Members.Length; i++) { pbfRelation.memids.Add(relation.Members[i].Id - relation.Members[i - 1].Id); pbfRelation.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[i].Role)); switch (relation.Members[i].Type) { case OsmGeoType.Node: pbfRelation.types.Add(Relation.MemberType.NODE); break; case OsmGeoType.Way: pbfRelation.types.Add(Relation.MemberType.WAY); break; case OsmGeoType.Relation: pbfRelation.types.Add(Relation.MemberType.RELATION); break; } } } return(pbfRelation); }