public static IFeatureClass CreateFeatureClass(fulcrumform form, string pathToGeoDB) { try { ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); ISpatialReference sr = geographicCoordinateSystem; IWorkspaceFactory2 wsf = new FileGDBWorkspaceFactoryClass(); IWorkspace2 workspace = (IWorkspace2)wsf.OpenFromFile(pathToGeoDB, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; string fcName = GetUniqueFeatureClassName(form.name, workspace); IFields fieldsCollection = new FieldsClass(); IFieldEdit newField = fieldsCollection as IFieldEdit; IFeatureClassDescription fcDesc = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass(); IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc; // create required fields using the required fields method IFields fields = ocDesc.RequiredFields; //Grab the GeometryDef from the shape field, edit it and give it back int shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName); IField shapeField = fields.get_Field(shapeFieldIndex); IGeometryDef geomDef = shapeField.GeometryDef; IGeometryDefEdit geomDefEdit = (IGeometryDefEdit)geomDef; geomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; geomDefEdit.GridCount_2 = 1; geomDefEdit.set_GridSize(0, 0); geomDefEdit.SpatialReference_2 = sr; IFields newFields = CreateNewFields(form, fields, sr); IFeatureClass featureClass = featureWorkspace.CreateFeatureClass (fcName, newFields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDesc.ShapeFieldName, ""); return featureClass; } catch (Exception e) { string errMsg = e.Message; return null; } }
public static bool FireUp(fulcrumform form, System.Windows.Controls.ProgressBar progBar, System.Windows.Controls.Label statusLabel) { try { statusLabel.Content = "Retrieving records..."; fulcrumrecords records = FulcrumCore.GetRecords(form.id); statusLabel.Content = "Processing records..."; FulcrumCore.ParseRecords(form, records, progBar); statusLabel.Content = "Creating layer..."; ArcStuff.DoArcStuff(form, records); statusLabel.Content = "Complete!"; return true; } catch (Exception e) { string ouch = e.Message; return false; } }
public static bool DoArcStuff(fulcrumform form, fulcrumrecords records) { try { IFeatureClass newFeatureClass = CreateFeatureClass(form, Properties.Settings.Default.pathToGeoDB); InsertFeatures(records, newFeatureClass); IFeatureLayer layer = CreateFeatureLayer(newFeatureClass, form.name); IMap map = ArcMap.Document.FocusMap; map.AddLayer((ILayer)layer); ArcMap.Document.ActiveView.Refresh(); return true; } catch (Exception e) { string bomb = e.Message; return false; } }
private static IFields CreateNewFields(fulcrumform form, IFields fields, ISpatialReference sr) { try { IFieldsEdit fieldsEdit = (IFieldsEdit)fields; foreach (fulcrumelement element in form.elements) { //!!! ToDo: Add data types as needed (numeric, etc) if (element.data_name != "") { IField field = new FieldClass(); IFieldEdit2 fieldEdit = (IFieldEdit2)field; switch (element.type) { case "Section": //a Section is a whole different animal it will have sub elements (children) like so, element.elements //!!!~but not dealing with them for now fieldEdit.Name_2 = element.data_name; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = element.label; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; // add field to field collection fieldsEdit.AddField(field); break; case "ClassificationField": fieldEdit.Name_2 = element.data_name; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = element.label; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; fieldsEdit.AddField(field); break; case "ChoiceField": fieldEdit.Name_2 = element.data_name; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = element.label; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; fieldsEdit.AddField(field); break; case "TextField": fieldEdit.Name_2 = element.data_name; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = element.label; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 255; fieldsEdit.AddField(field); break; case "DateTimeField": //if the name happens to be Date we need to override it if (element.data_name == "date") { fieldEdit.Name_2 = "record_date"; } else { fieldEdit.Name_2 = element.data_name; } fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = element.label; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 50; fieldsEdit.AddField(field); break; case "PhotoField": fieldEdit.Name_2 = photoFieldName; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = element.label; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 200; //If we want to stuff the image in a raster field, here's a start /*!!! IRasterDef rasterDef = new RasterDefClass(); rasterDef.Description = "PictureField"; rasterDef.IsManaged = true; rasterDef.IsRasterDataset = true; //rasterDef.SpatialReference = sr; This is throwing an error fieldEdit.RasterDef = rasterDef; fieldEdit.Type_2 = esriFieldType.esriFieldTypeRaster; */ fieldsEdit.AddField(field); break; case "Label": //skip it default: //a data type we haven't accounted for... //don't create a field for it break; } field = null; fieldEdit = null; } } fields = (IFields)fieldsEdit; // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); // The enumFieldError enumerator can be inspected at this point to determine // which fields were modified during validation. if (enumFieldError != null) { enumFieldError.Reset(); IFieldError err = enumFieldError.Next(); while (!(err == null)) { //!!!ToDo: //we need to do something here cause the feature class will not be created with invalid fields Console.WriteLine(err.FieldError + " at index " + err.FieldIndex); err = enumFieldError.Next(); } } return validatedFields; } catch (Exception e) { string choke = e.Message; return null; } }
private static bool GetFieldNameAndTypeFromFormDefinition(fulcrumform form, string fieldId, out string fieldName, out string fieldType) { try { foreach (fulcrumelement element in form.elements) { if (element.elements == null) { if (element.key == fieldId) { fieldName = element.data_name; fieldType = element.type; return true; } } else { //we have nested elements //ToDo: implement recursion to get all, we are only going one level here foreach (fulcrumelement childElement in element.elements) { if (childElement.key == fieldId) { fieldName = childElement.data_name; fieldType = childElement.type; return true; } } } } fieldName = ""; fieldType = ""; return false; } catch (Exception e) { string ouch = e.Message; fieldName = ""; fieldType = ""; return false; } }
public static bool ParseRecords(fulcrumform form, fulcrumrecords fulcrumRecords, System.Windows.Controls.ProgressBar progBar) { try { progBar.Maximum = fulcrumRecords.records.Count; //WPF Prog Bar, WTF?! //System.Windows.Media.Duration duration = new Duration(TimeSpan.FromSeconds(10)); //System.Windows.Media.Animation.DoubleAnimation doubleanimation = new System.Windows.Media.Animation.DoubleAnimation(100.0, duration); //progBar.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, doubleanimation); int count = 1; foreach (fulcrumrecord record in fulcrumRecords.records) { progBar.Value = count; string recordId = record.id; foreach (string key in record.form_values.Keys) { fulcrumattribute recordAttribute = new fulcrumattribute(); recordAttribute.recordID = recordId; string fieldname; string fieldType; if (GetFieldNameAndTypeFromFormDefinition(form, key, out fieldname, out fieldType)) { //if the value is a set of sub objects, we'll get another dictionary //if the value is a javascript array, we'll get an arraylist //otherwise we'll get the string value object o = record.form_values[key]; if (o is Dictionary<string, object>) { //I think!!! the only time this will happen is when the value comes from a choice list //so grab the value from the choice_values item Dictionary<string, object> d = ((Dictionary<string, object>)o); //ToDo: if this is a value\label pair we're always going to ge the coded value //is there a way to determine if it is a coded value??? //e.g. coded value = 002, label = Krazy Kool ArrayList a = (ArrayList)d["choice_values"]; recordAttribute.fieldName = fieldname; recordAttribute.fieldValue = a[0].ToString(); } else if (o is ArrayList) { foreach (object child in ((ArrayList)o)) { if (child is string) { recordAttribute.fieldName = fieldname; recordAttribute.fieldValue = child.ToString(); //Console.WriteLine(fieldname + ": " + child.ToString()); } else if (child is Dictionary<string, object>) { Dictionary<string, object> childDict = (Dictionary<string, object>)child; foreach (string childKey in childDict.Keys) { //ToDo: we are writing the link to the photo as well as //grabbing the photo thumb as an image and adding it to the records' photo collection //and saving it to disk //you will likely want to choose from these options, downloading is expensive if (fieldType == "PhotoField") { string photoId = childDict["photo_id"].ToString(); string thumbUrl = childDict["thumbnail"].ToString(); string url = childDict["url"].ToString(); Image img = GetFulcrumImage(thumbUrl); record.photos.Add(img); string pathToPhoto = Properties.Settings.Default.pathToPhotoFolder + "\\" + photoId + ".jpg"; string linkToThumb = "http://web.fulcrumapp.com/photos/" + photoId + "/thumbnail"; string linkToPhoto = " http://web.fulcrumapp.com/photos/" + photoId; img.Save(pathToPhoto, System.Drawing.Imaging.ImageFormat.Jpeg); record.photolinks.Add(linkToPhoto); } else { //assuming all values are now strings recordAttribute.fieldName = childKey.ToString(); recordAttribute.fieldValue = childDict[childKey].ToString(); //Console.WriteLine(childKey.ToString() + ": " + childDict[childKey].ToString()); } } } } } else { recordAttribute.fieldName = fieldname; recordAttribute.fieldValue = o.ToString(); //Console.WriteLine(fieldname + ": " + o.ToString()); } record.attributes.Add(recordAttribute); } else { //couldnt get the field name or type } } count++; } return true; } catch (Exception e) { string ouch = e.Message; return false; } }