Categories: Blog Posted by al on 4/1/2011 1:00 AM | Comments (0)

There are many API’s that use the OSM rest end points to create features, I didn’t find much in C# or VB.NET and I wanted to make sure all the changesets I uploaded got delete it. I use the ArcGIS Editor for ArcMap as a baseline to write this code to make sure I clean up a changeset.

I thought to put the code here for people that is looking in how to download a changeset from OSM or delete nodes.

   1:  public static void DeleteChangeSet(string ID, string sUsername, string sPassword)
   2:          {
   3:              
   4:              WebRequest request = WebRequest.Create("http://www.openstreetmap.org/api/0.6/changeset/"+ID+"/download");
   5:              Console.WriteLine("Called ChangeSet " + ID);
   6:   
   7:              request.Timeout = Timeout.Infinite;
   8:   
   9:              WebResponse response = request.GetResponse();
  10:              Stream dataStream = response.GetResponseStream();
  11:   
  12:              StreamReader readerst = new StreamReader(dataStream);
  13:              // Read the content.         
  14:              string sResults = readerst.ReadToEnd();
  15:   
  16:              Console.WriteLine("String Back from the ChangeSet with bytes " + sResults.Length);
  17:   
  18:              XmlDocument xmlDoc = new XmlDocument();
  19:              xmlDoc.LoadXml(sResults);
  20:   
  21:              // Create the changeset            
  22:              WebRequest client = WebRequest.Create("http://www.openstreetmap.org/api/0.6/changeset/create");
  23:              client.Method = "PUT";
  24:              // Start a delete changeset session
  25:              //http://www.openstreetmap.org/api/0.6/changeset/create
  26:              string sXmlChangeSet = "<?xml version=\"1.0\" encoding=\"us-ascii\"?><osm xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"0.6\" generator=\"ArcGIS\"><changeset id=\"0\"><tag v=\"ArcGIS Editor for OpenStreetMap (1.1 beta4)\" k=\"created_by\" /><tag v=\"Cleaning Arkansas from changeset" + ID + "\" k=\"comment\" /></changeset></osm>";
  27:              client.Credentials = new System.Net.NetworkCredential(sUsername, sPassword);
  28:   
  29:              //string sChangeSetID = client.UploadString(new Uri("http://www.openstreetmap.org/api/0.6/changeset/create"), sXmlChangeSet);
  30:              Stream requestStreamclient = client.GetRequestStream();            
  31:              StreamWriter mywriter = new StreamWriter(requestStreamclient);
  32:   
  33:              mywriter.Write(sXmlChangeSet);
  34:              mywriter.Close();
  35:   
  36:              WebResponse clientResponse = client.GetResponse();
  37:              Stream readStream = clientResponse.GetResponseStream();
  38:              StreamReader streamReader = new StreamReader(readStream);
  39:              string sChangeSetID = streamReader.ReadToEnd();
  40:              
  41:              Console.WriteLine("Created ChangeSetID " + sChangeSetID);
  42:   
  43:              // Get the nodes and start deleting
  44:              XmlNodeList nodes = xmlDoc.ChildNodes.Item(1).ChildNodes;
  45:              dataStream.Close();
  46:              readerst.Close();
  47:              streamReader.Close();
  48:   
  49:              foreach (XmlNode node in nodes)
  50:              {
  51:                  string sID = node.ChildNodes.Item(0).Attributes[0].Value;
  52:                  string sVersion = node.ChildNodes.Item(0).Attributes["version"].Value;
  53:                  string lat = node.ChildNodes.Item(0).Attributes["lat"].Value;
  54:                  string lon = node.ChildNodes.Item(0).Attributes["lon"].Value;
  55:                                  
  56:                  //<?xml version="1.0" encoding="utf-8"?><osm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="0.6"><node lat="33.8952356958747" visible="true" lon="-117.309384882913" changeset="7717855" version="1" id="1225499422" /></osm>
  57:                  string sXmlToDelete = "<?xml version=\"1.0\" encoding=\"utf-8\"?><osm xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"0.6\"><node lat=\""+lat+"\" lon=\""+lon+"\" id=\"" + sID + "\" version=\"" + sVersion + "\" changeset=\"" + sChangeSetID + "\"/></osm>";
  58:   
  59:                  Console.WriteLine("Deleting Node " + sID);
  60:   
  61:                  WebRequest delete = WebRequest.Create("http://www.openstreetmap.org/api/0.6/node/" + sID);
  62:                  delete.Credentials = new System.Net.NetworkCredential(sUsername, sPassword);
  63:                  delete.Method = "DELETE";
  64:   
  65:                  Stream requestStream = delete.GetRequestStream();
  66:                  StreamWriter stwriter = new StreamWriter(requestStream);
  67:   
  68:                  stwriter.Write(sXmlToDelete);
  69:                  stwriter.Close();
  70:   
  71:                  WebResponse deleteResponse = null;
  72:                  try
  73:                  {
  74:                      deleteResponse = delete.GetResponse();
  75:                  }
  76:                  catch (Exception e) { Console.WriteLine("Cannot delete node " + e.Message); }
  77:   
  78:                  stwriter.Close();
  79:                  if ( deleteResponse != null )
  80:                      deleteResponse.Close();
  81:              }
  82:   
  83:              // Close change set 
  84:              string sClose = "http://www.openstreetmap.org//api/0.6/changeset/" + sChangeSetID + "/close";
  85:              WebRequest closeRequest = WebRequest.Create(sClose);
  86:              closeRequest.Method = "PUT";
  87:              closeRequest.Credentials = new System.Net.NetworkCredential(sUsername, sPassword);
  88:   
  89:              WebResponse closeResponse = closeRequest.GetResponse();
  90:              closeResponse.Close();
  91:   
  92:              Console.WriteLine("Close ChangeSetID " + sChangeSetID);
  93:   
  94:          }
blog comments powered by Disqus