Skip to content

Instantly share code, notes, and snippets.

@brightrain
Created December 12, 2012 21:36
Show Gist options
  • Save brightrain/4271841 to your computer and use it in GitHub Desktop.
Save brightrain/4271841 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using System.IO;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace AGOLFeatureServiceUpdater
{
class Program
{
private static LicenseInitializer aoLicenseInitializer = new AGOLFeatureServiceUpdater.LicenseInitializer();
static void Main(string[] args)
{
try
{
DeleteAllFeaturesFromFeatureService();
aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcView },
new esriLicenseExtensionCode[] { });
IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass();
IFeatureWorkspace ws = (IFeatureWorkspace)workspaceFactory.OpenFromFile("C:\\Dropbox\\sandbox\\rec.gdb", 0);
IFeatureClass recSitesFeatureClass = ws.OpenFeatureClass("wenatchee_rec_sites");
int typeFieldIndex = recSitesFeatureClass.FindField("TYPE");
int areaFieldIndex = recSitesFeatureClass.FindField("AREA");
int perimeterFieldIndex = recSitesFeatureClass.FindField("PERIMETER");
int nameFieldIndex = recSitesFeatureClass.FindField("REC_NAME");
//string featureJSONArrayString = "[";
IFeatureCursor fcur = recSitesFeatureClass.Search(null, false);
IFeature recFeature = fcur.NextFeature();
while (recFeature != null)
{
IPoint pt = (IPoint)recFeature.Shape;
string jsonFeature = string.Empty;
jsonFeature += "[{'geometry': {'x': " + pt.X + ",'y': " + pt.Y + ",'spatialReference': {'wkid': 4326}},'attributes': {'TYPE': '";
jsonFeature += recFeature.get_Value(typeFieldIndex) + "','AREA': '" + recFeature.get_Value(areaFieldIndex);
jsonFeature += "','PERIMETER': '" + "','REC_NAME': '" + recFeature.get_Value(nameFieldIndex) + "'}}]";
//string jsonf = "[{'geometry': {'x': -120.663242,'y': 47.784908,'spatialReference': {'wkid': 4326}},"
//+ "'attributes': {'TYPE': 'Picnic Area','AREA': '0','PERIMETER': '0','REC_NAME': 'Chumstick!!!'}}]";
string reqString = "http://services.arcgis.com/xxxxxxxxx/ArcGIS/rest/services/WenatcheeNationalForestRecSites/FeatureServer/0/addFeatures?f=json&features=" + jsonFeature;
HttpWebRequest req = WebRequest.Create(new Uri(reqString)) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/json";
// Encode the parameters as form data:
byte[] formData = UTF8Encoding.UTF8.GetBytes(reqString);
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Pick up the response:
string result = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
Console.WriteLine(result.ToString());
}
recFeature = fcur.NextFeature();
}
}
catch(Exception e)
{
string ouch = e.Message;
return;
}
finally
{
aoLicenseInitializer.ShutdownApplication();
}
}
static Boolean DeleteAllFeaturesFromFeatureService()
{
try
{
string reqString = "http://services.arcgis.com/xxxxxxxxx/ArcGIS/rest/services/WenatcheeNationalForestRecSites/FeatureServer/0/deleteFeatures?f=json&where=OBJECTID > -1";
HttpWebRequest req = WebRequest.Create(new Uri(reqString)) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/json";
// Encode the parameters as form data:
byte[] formData = UTF8Encoding.UTF8.GetBytes(reqString);
//req.contentLength = formData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Pick up the response:
string result = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
Console.WriteLine(result.ToString());
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment