/// <summary> /// Encodes the nodes into the given block. /// </summary> public static void Encode(this PrimitiveBlock block, Dictionary <string, int> reverseStringTable, List <OsmGeo> osmGeos, bool compressed) { var groupIdx = 0; var i = 0; if (block.stringtable != null && block.stringtable.s != null) { block.stringtable.s.Clear(); } while (i < osmGeos.Count) { PrimitiveGroup group = null; if (groupIdx < block.primitivegroup.Count) { // get the existing group and clear it's data. group = block.primitivegroup[groupIdx]; if (group == null) { group = new PrimitiveGroup(); } if (group.dense != null) { if (group.dense.denseinfo != null) { if (group.dense.denseinfo.changeset != null) { group.dense.denseinfo.changeset.Clear(); } if (group.dense.denseinfo.timestamp != null) { group.dense.denseinfo.timestamp.Clear(); } if (group.dense.denseinfo.uid != null) { group.dense.denseinfo.uid.Clear(); } if (group.dense.denseinfo.user_sid != null) { group.dense.denseinfo.user_sid.Clear(); } if (group.dense.denseinfo.version != null) { group.dense.denseinfo.version.Clear(); } } if (group.dense.id != null) { group.dense.id.Clear(); } if (group.dense.keys_vals != null) { group.dense.keys_vals.Clear(); } if (group.dense.lat != null) { group.dense.lat.Clear(); } if (group.dense.lon != null) { group.dense.lon.Clear(); } } if (group.changesets != null) { group.changesets.Clear(); } //if (group.nodes != null) { group.nodes.Clear(); } if (group.ways != null) { group.ways.Clear(); } if (group.relations != null) { group.relations.Clear(); } } else { // add a new group. group = new PrimitiveGroup(); block.primitivegroup.Add(group); } // build group. var currentNodeCount = 0; var groupType = osmGeos[i].Type; var previousNode = new OsmSharp.Node { Id = 0, Longitude = 0, Latitude = 0, ChangeSetId = 0, TimeStamp = null, UserId = 0, UserName = null, Version = 0 }; if (groupType == OsmGeoType.Node && compressed && group.dense == null) { group.dense = new DenseNodes { denseinfo = new DenseInfo() }; } while (i < osmGeos.Count && osmGeos[i].Type == groupType) { switch (groupType) { case OsmGeoType.Node: if (compressed) { var currentNode = osmGeos[i] as OsmSharp.Node; Encoder.EncodeDenseNode(block, reverseStringTable, group.dense, currentNode, previousNode); previousNode = currentNode; } else { if (currentNodeCount < group.nodes.Count) { // overwrite existing. Encoder.EncodeNode(block, reverseStringTable, group.nodes[currentNodeCount], osmGeos[i] as OsmSharp.Node); } else { // write new. group.nodes.Add(Encoder.EncodeNode(block, reverseStringTable, osmGeos[i] as OsmSharp.Node)); } currentNodeCount++; } break; case OsmGeoType.Way: group.ways.Add(Encoder.EncodeWay(block, reverseStringTable, osmGeos[i] as OsmSharp.Way)); break; case OsmGeoType.Relation: group.relations.Add(Encoder.EncodeRelation(block, reverseStringTable, osmGeos[i] as OsmSharp.Relation)); break; } i++; } // remove obsolete nodes. if (group.nodes != null) { while (currentNodeCount < group.nodes.Count) { group.nodes.RemoveAt(currentNodeCount); } } // move to the next group. groupIdx++; } while (groupIdx < block.primitivegroup.Count) { block.primitivegroup.RemoveAt(groupIdx); } }
/// <summary> /// Encodes the nodes into the given block. /// </summary> public static void Encode(this PrimitiveBlock block, Dictionary <string, int> reverseStringTable, List <OsmGeo> osmGeos) { var groupIdx = 0; var i = 0; var nodeCount = 0; if (block.stringtable != null && block.stringtable.s != null) { block.stringtable.s.Clear(); } while (i < osmGeos.Count) { PrimitiveGroup group = null; if (groupIdx < block.primitivegroup.Count) { // get the existing group and clear it's data. group = block.primitivegroup[groupIdx]; if (group == null) { group = new PrimitiveGroup(); } if (group.dense != null) { if (group.dense.denseinfo != null) { if (group.dense.denseinfo.changeset != null) { group.dense.denseinfo.changeset.Clear(); } if (group.dense.denseinfo.timestamp != null) { group.dense.denseinfo.timestamp.Clear(); } if (group.dense.denseinfo.uid != null) { group.dense.denseinfo.uid.Clear(); } if (group.dense.denseinfo.user_sid != null) { group.dense.denseinfo.user_sid.Clear(); } if (group.dense.denseinfo.version != null) { group.dense.denseinfo.version.Clear(); } } if (group.dense.id != null) { group.dense.id.Clear(); } if (group.dense.keys_vals != null) { group.dense.keys_vals.Clear(); } if (group.dense.lat != null) { group.dense.lat.Clear(); } if (group.dense.lon != null) { group.dense.lon.Clear(); } } if (group.changesets != null) { group.changesets.Clear(); } //if (group.nodes != null) { group.nodes.Clear(); } if (group.ways != null) { group.ways.Clear(); } if (group.relations != null) { group.relations.Clear(); } } else { // add a new group. group = new PrimitiveGroup(); block.primitivegroup.Add(group); } // build group. var groupType = osmGeos[i].Type; var current = osmGeos[i]; while (i < osmGeos.Count && osmGeos[i].Type == groupType) { switch (groupType) { case OsmGeoType.Node: if (group.nodes.Count > nodeCount) { // overwrite existing. Encoder.EncodeNode(block, reverseStringTable, group.nodes[nodeCount], osmGeos[i] as OsmSharp.Node); } else { // write new. group.nodes.Add(Encoder.EncodeNode(block, reverseStringTable, osmGeos[i] as OsmSharp.Node)); } nodeCount++; break; case OsmGeoType.Way: group.ways.Add(Encoder.EncodeWay(block, reverseStringTable, osmGeos[i] as OsmSharp.Way)); break; case OsmGeoType.Relation: group.relations.Add(Encoder.EncodeRelation(block, reverseStringTable, osmGeos[i] as OsmSharp.Relation)); break; } i++; } // remove obsolete nodes. if (group.nodes != null) { while (nodeCount < group.nodes.Count) { group.nodes.RemoveAt(nodeCount); } } // move to the next group. groupIdx++; } while (groupIdx < block.primitivegroup.Count) { block.primitivegroup.RemoveAt(groupIdx); } }