private static void FlatBinstanceToViewModel(this BinViewModel inViewModel) { FlatBin flat = (FlatBin)inViewModel.Binstance; inViewModel.CribLength = flat.CribLength.ToString(); inViewModel.CribWidth = flat.CribWidth.ToString(); }
public static FlatBin MapBinToFlatType(this BinViewModel inViewModel, IBinstance bin) { FlatBin fbin = new FlatBin(bin); double d; try { fbin.CribLength = double.TryParse(inViewModel.CribLength, out d) ? d : 0.0; } catch (Exception) { fbin.CribLength = 0; } try { fbin.CribWidth = double.TryParse(inViewModel.CribWidth, out d) ? d : 0; } catch (Exception) { fbin.CribLength = 0; } return(fbin); }
//handle bin type specific mapping below public static FlatBin MapBinToFlatType(this Feature inFeature, IBinstance bin) { FlatBin fbin = new FlatBin(bin); double d; try { string s = inFeature.Attributes["crib_height"].ToString(); fbin.CribLength = double.TryParse(s, out d) ? d : 0.0; } catch (Exception) { fbin.CribLength = 0; } try { string s = inFeature.Attributes["crib_width"].ToString(); fbin.CribWidth = double.TryParse(s, out d) ? d : 0; } catch (Exception) { fbin.CribLength = 0; } return(fbin); }
public static async Task <ArcCrudEnum> EditBin(BinViewModel binViewModel) { IBinstance bin = binViewModel.Binstance; ArcGISFeature featureToEdit = binViewModel.ArcGISFeature; try { await featureToEdit.LoadAsync(); featureToEdit.Attributes["identifier"] = bin.Identifier; featureToEdit.Attributes["modified_by"] = binViewModel.EmpoyeeNumber; switch (bin.BinType) { case BinTypeEnum.RoundStorage: featureToEdit.Attributes["bin_type"] = "round_storage"; break; case BinTypeEnum.GravityWagon: featureToEdit.Attributes["bin_type"] = "gravity_wagon"; break; case BinTypeEnum.PolygonStructure: featureToEdit.Attributes["bin_type"] = "polygon_structure"; break; case BinTypeEnum.FlatStructure: featureToEdit.Attributes["bin_type"] = "flat_structure"; break; } featureToEdit.Attributes["year_collected"] = bin.YearCollected; if (bin.IsLeased.HasValue) { featureToEdit.Attributes["owned_or_leased"] = bin.IsLeased.Value ? "leased" : "owned"; } if (bin.HasDryingDevice.HasValue) { featureToEdit.Attributes["drying_device"] = bin.HasDryingDevice.Value ? "true" : "false"; } if (bin.HasGrainHeightIndicator.HasValue) { featureToEdit.Attributes["bin_level_indicator_device"] = bin.HasGrainHeightIndicator.Value ? "true" : "false"; } switch (bin.LadderType) { case Ladder.None: featureToEdit.Attributes["ladder_type"] = "none"; break; case Ladder.Ladder: featureToEdit.Attributes["ladder_type"] = "ladder"; break; case Ladder.Stairs: featureToEdit.Attributes["ladder_type"] = "stairs"; break; } featureToEdit.Attributes["notes"] = bin.Notes; double dr; //double.TryParse(bin.BinVolume, out dr); //featureToEdit.Attributes["bin_volume"] = dr; //bin type specific logic below Type t = bin.GetType(); if (bin.BinType == BinTypeEnum.FlatStructure) { if (t.Equals(typeof(FlatBin))) { FlatBin flat = (FlatBin)bin; featureToEdit.Attributes["crib_height"] = flat.CribLength; featureToEdit.Attributes["crib_width"] = flat.CribWidth; } } else if (bin.BinType == BinTypeEnum.GravityWagon) { if (t.Equals(typeof(GravityBin))) { GravityBin gravityBin = (GravityBin)bin; featureToEdit.Attributes["chute_length"] = gravityBin.ChuteLength; featureToEdit.Attributes["hopper_height"] = gravityBin.HopperHeight; featureToEdit.Attributes["rectangle_height"] = gravityBin.RectangleHeight; featureToEdit.Attributes["rectangle_length"] = gravityBin.RectangleLength; featureToEdit.Attributes["rectangle_width"] = gravityBin.RectangleWidth; } } else if (bin.BinType == BinTypeEnum.PolygonStructure) { if (t.Equals(typeof(PolygonBin))) { PolygonBin polygonBin = (PolygonBin)bin; featureToEdit.Attributes["side_height"] = polygonBin.SideHeight; featureToEdit.Attributes["side_width"] = polygonBin.SideWidth; featureToEdit.Attributes["number_of_sides"] = polygonBin.NumberOfSides; } } else if (bin.BinType == BinTypeEnum.RoundStorage) { if (t.Equals(typeof(PolygonBin))) { RoundBin round = (RoundBin)bin; if (round.HasHopper.HasValue) { featureToEdit.Attributes["has_hopper"] = round.HasHopper.Value ? "true" : "false"; } featureToEdit.Attributes["radius"] = round.Radius; featureToEdit.Attributes["wall_height"] = round.WallHeight; featureToEdit.Attributes["roof_height"] = round.RoofHeight; featureToEdit.Attributes["hopper_height"] = round.HopperHeight; } } // can't be null if (binViewModel.Binstance.YTYDatas == null) { binViewModel.Binstance.YTYDatas = new List <YTYData>(); } //use data in _binViewModel System.Diagnostics.Debug.Print("Feature can edit attachments" + (featureToEdit.CanEditAttachments ? "Yes" : "No")); //-------- Formatting -------- //create json string jsonString = JsonConvert.SerializeObject(binViewModel.Binstance.YTYDatas); //System.Diagnostics.Debug.Print(jsonString); //System.Diagnostics.Debug.Print(((Binstance)(binViewModel.Binstance)).YTYDatasString()); // convert json to byte array byte[] byteArray = Encoding.UTF8.GetBytes(jsonString); //-------- ARC Connection -------- //remove old YTYData List <Attachment> attachmentsToRemove = new List <Attachment>(); IReadOnlyList <Attachment> attachments = await featureToEdit.GetAttachmentsAsync(); foreach (Attachment attachment in attachments) { System.Diagnostics.Debug.Print(attachment.Name); if (attachment.Name.Equals(YTY_FILE_NAME)) { System.Diagnostics.Debug.Print("Found YTY attachment"); attachmentsToRemove.Add(attachment); } } System.Diagnostics.Debug.Print("Attachments to remove:"); foreach (Attachment attachment in attachments) { System.Diagnostics.Debug.Print(attachment.Name); } if (attachmentsToRemove.Any()) { //update the json file await featureToEdit.UpdateAttachmentAsync(attachmentsToRemove.First(), YTY_FILE_NAME, "application/json", byteArray); } _featureTable = (ServiceFeatureTable)featureToEdit.FeatureTable; // update feature after attachment added await _featureTable.UpdateFeatureAsync(featureToEdit); //agsFeature System.Diagnostics.Debug.Print("Feature table updated"); // push to ArcGIS Online feature service IReadOnlyList <EditResult> editResults = await _featureTable.ApplyEditsAsync(); System.Diagnostics.Debug.Print("Arc updated"); foreach (var er in editResults) { if (er.CompletedWithErrors) { // handle error (er.Error.Message) return(ArcCrudEnum.Failure); } } return(ArcCrudEnum.Success); } catch (ArcGISWebException) { return(ArcCrudEnum.Exception); } }
public static async Task <ArcCrudEnum> AddBin(BinViewModel binViewModel) { IBinstance bin = binViewModel.Binstance; IEnumerable <YTYData> yTY = bin.YTYDatas; try { await _featureTable.LoadAsync(); var attributes = new Dictionary <string, object>(); attributes.Add("identifier", bin.Identifier); attributes.Add("created_by", bin.CreatedBy); attributes.Add("modified_by", bin.ModifiedBy); switch (bin.BinType) { case BinTypeEnum.RoundStorage: attributes.Add("bin_type", "round_storage"); break; case BinTypeEnum.GravityWagon: attributes.Add("bin_type", "gravity_wagon"); break; case BinTypeEnum.PolygonStructure: attributes.Add("bin_type", "polygon_structure"); break; case BinTypeEnum.FlatStructure: attributes.Add("bin_type", "flat_structure"); break; } attributes.Add("year_collected", bin.YearCollected); if (bin.IsLeased.HasValue) { attributes.Add("owned_or_leased", bin.IsLeased.Value ? "leased" : "owned"); } if (bin.HasDryingDevice.HasValue) { attributes.Add("drying_device", bin.HasDryingDevice.Value ? "true" : "false"); } if (bin.HasGrainHeightIndicator.HasValue) { attributes.Add("bin_level_indicator_device", bin.HasGrainHeightIndicator.Value ? "true" : "false"); } switch (bin.LadderType) { case Ladder.None: attributes.Add("ladder_type", "none"); break; case Ladder.Ladder: attributes.Add("ladder_type", "ladder"); break; case Ladder.Stairs: attributes.Add("ladder_type", "stairs"); break; } attributes.Add("notes", bin.Notes); //bin type specific logic below Type t = bin.GetType(); if (bin.BinType == BinTypeEnum.FlatStructure) { if (t.Equals(typeof(FlatBin))) { FlatBin flat = (FlatBin)bin; attributes.Add("crib_height", flat.CribLength); attributes.Add("crib_width", flat.CribWidth); } } else if (bin.BinType == BinTypeEnum.GravityWagon) { if (t.Equals(typeof(GravityBin))) { GravityBin gravityBin = (GravityBin)bin; attributes.Add("chute_length", gravityBin.ChuteLength); attributes.Add("hopper_height", gravityBin.HopperHeight); attributes.Add("rectangle_height", gravityBin.RectangleHeight); attributes.Add("rectangle_length", gravityBin.RectangleLength); attributes.Add("rectangle_width", gravityBin.RectangleWidth); } } else if (bin.BinType == BinTypeEnum.PolygonStructure) { if (t.Equals(typeof(PolygonBin))) { PolygonBin polygonBin = (PolygonBin)bin; attributes.Add("side_height", polygonBin.SideHeight); attributes.Add("side_width", polygonBin.SideWidth); attributes.Add("number_of_sides", polygonBin.NumberOfSides); } } else if (bin.BinType == BinTypeEnum.RoundStorage) { if (t.Equals(typeof(RoundBin))) { RoundBin round = (RoundBin)bin; if (round.HasHopper.HasValue) { attributes.Add("has_hopper", round.HasHopper.Value ? "true" : "false"); } attributes.Add("radius", round.Radius); attributes.Add("wall_height", round.WallHeight); attributes.Add("roof_height", round.RoofHeight); attributes.Add("hopper_height", round.HopperHeight); } } MapPoint mp; double lat = 0; double lon = 0; try { var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 1000; var position = await locator.GetPositionAsync(); if (position == null) { Console.WriteLine("Location Service error"); } lat = position.Latitude; lon = position.Longitude; } catch { } string lonS = lon.ToString(); string[] split = lonS.Split('.'); var charArray = split[1].ToCharArray(); //seperate good location, make third if necassary string first = charArray[0].ToString(); string second = charArray[1].ToString(); string third = charArray[2].ToString(); Random random = new Random(); int addition = random.Next(25, 75); string additionS = addition.ToString(); string newLong = split[0] + '.' + first + second + third + additionS; //latitude string latS = lat.ToString(); string[] splitL = latS.Split('.'); var charArrayL = splitL[1].ToCharArray(); //seperate good location, make third if necassary string firstL = charArrayL[0].ToString(); string secondL = charArrayL[1].ToString(); string thirdL = charArrayL[2].ToString(); int additionL = random.Next(25, 75); string additionSL = additionL.ToString(); string newLat = splitL[0] + '.' + firstL + secondL + thirdL + additionSL; double clusteredLongitude = double.Parse(newLong); double clusteredLatitude = double.Parse(newLat); mp = new MapPoint(clusteredLongitude, clusteredLatitude); binViewModel.ArcGISFeature = (ArcGISFeature)_featureTable.CreateFeature(attributes, mp); await _featureTable.AddFeatureAsync(binViewModel.ArcGISFeature); await binViewModel.ArcGISFeature.LoadAsync(); if (DeviceInfo.DeviceType == DeviceType.Physical) { using (var memoryStream = new MemoryStream()) { bin.Image.GetStream().CopyTo(memoryStream); // add an attachment - pass the name, mime type, and attachment data (bytes, etc.) await binViewModel.ArcGISFeature.AddAttachmentAsync("Photo.jpg", "image/jpg", memoryStream.ToArray()); //agsFeature } } if (yTY == null) { yTY = new List <YTYData>(); } //create json string jsonString = JsonConvert.SerializeObject(yTY); byte[] byteArray = Encoding.UTF8.GetBytes(jsonString); List <Attachment> attachmentsToRemove = new List <Attachment>(); IReadOnlyList <Attachment> attachments = await binViewModel.ArcGISFeature.GetAttachmentsAsync(); foreach (Attachment attachment in attachments) { if (attachment.Name.Equals(YTY_FILE_NAME)) { attachmentsToRemove.Add(attachment); } } await binViewModel.ArcGISFeature.DeleteAttachmentsAsync(attachmentsToRemove); await binViewModel.ArcGISFeature.AddAttachmentAsync(YTY_FILE_NAME, "application/json", byteArray); //agsFeature await _featureTable.UpdateFeatureAsync(binViewModel.ArcGISFeature); //agsFeature // push to ArcGIS Online feature service IReadOnlyList <EditResult> editResults = await _featureTable.ApplyEditsAsync(); // check results for errors foreach (var er in editResults) { if (er.CompletedWithErrors) { return(ArcCrudEnum.Failure); } } return(ArcCrudEnum.Success); } catch (ArcGISWebException) { return(ArcCrudEnum.Exception); } }
public BinViewModel(ArcGISFeature feature) { Title = "View Bin"; New = false; Edit = false; ArcGISFeature = feature; Binstance = ArcGISFeature.MapToBin(); FeatureMapBinType(); //display contents information //SetYTY(); //if (Binstance.YTYDatas.Count > 0) //{ // YTYData = Binstance.YTYDatas[0]; //foreach (var year in dateRange) //{ // if (year == YTYData.CropYear) // { // CropYear = year; // break; // } //} //Crop = YTYData.Crop; //GrainHeight = YTYData.GrainHeight.ToString(); //GrainHopperHeight = YTYData.GrainHopperHeight.ToString(); //GrainConeHeight = YTYData.ConeHeight.ToString(); //MoisturePercent = YTYData.MoistureOfGrain.ToString(); //MoistureFactor = YTYData.MoistureFactor.ToString(); //TestWeight = YTYData.TestWeight.ToString(); //PackFactor = YTYData.PackFactor.ToString(); //DockagePercent = YTYData.DockagePercent.ToString(); //DockageFactor = YTYData.DockageFactor.ToString(); ////GrainVolume = YTYData.TotalVolume.ToString(); //switch (YTYData.ConversionFactor) //{ // case 0.4: // ConversionFactor = 0; // break; // case 0.8: // ConversionFactor = 1; // break; // default: // ConversionFactor = -1; // break; //} //ShellFactor = YTYData.ShellFactor.ToString(); //TotalDeductionVolume = YTYData.TotalDeductionVolume.ToString(); //ContentsNotes = YTYData.Notes; //} //dsiplay general information YearCollected = Binstance.YearCollected; CreatedBy = Binstance.CreatedBy; ModifiedBy = Binstance.ModifiedBy; Identifier = Binstance.Identifier; switch (Binstance.IsLeased) { case true: Owned = 0; break; case false: Owned = 1; break; default: Owned = -1; break; } switch (Binstance.HasDryingDevice) { case true: HasDryingDevice = 0; break; case false: HasDryingDevice = 1; break; default: HasDryingDevice = -1; break; } switch (Binstance.HasGrainHeightIndicator) { case true: HasGrainHeightIndicator = 0; break; case false: HasGrainHeightIndicator = 1; break; default: HasGrainHeightIndicator = -1; break; } switch (Binstance.LadderType) { case Ladder.None: LadderType = 0; break; case Ladder.Stairs: LadderType = 1; break; case Ladder.Ladder: LadderType = 2; break; } Notes = Binstance.Notes; //display capacity information Type t; switch (Binstance.BinType) { case BinTypeEnum.FlatStructure: BinType = 0; t = Binstance.GetType(); if (t.Equals(typeof(FlatBin))) { FlatBin fbin = (FlatBin)Binstance; CribWidth = fbin.CribWidth.ToString(); CribLength = fbin.CribLength.ToString(); } break; case BinTypeEnum.GravityWagon: BinType = 1; t = Binstance.GetType(); if (t.Equals(typeof(GravityBin))) { GravityBin gbin = (GravityBin)Binstance; RectangleHeight = gbin.RectangleHeight.ToString(); RectangleLength = gbin.RectangleLength.ToString(); RectangleWidth = gbin.RectangleWidth.ToString(); ChuteLength = gbin.ChuteLength.ToString(); HopperHeight = gbin.HopperHeight.ToString(); } break; case BinTypeEnum.PolygonStructure: BinType = 2; t = Binstance.GetType(); if (t.Equals(typeof(PolygonBin))) { PolygonBin pbin = (PolygonBin)Binstance; SideHeight = pbin.SideHeight.ToString(); SideWidth = pbin.SideWidth.ToString(); numberOfSides = pbin.NumberOfSides.ToString(); } break; case BinTypeEnum.RoundStorage: BinType = 3; t = Binstance.GetType(); if (t.Equals(typeof(RoundBin))) { RoundBin rbin = (RoundBin)Binstance; Radius = rbin.Radius.ToString(); RoofHeight = rbin.RoofHeight.ToString(); WallHeight = rbin.WallHeight.ToString(); RoundHopperHeight = rbin.HopperHeight.ToString(); } break; case BinTypeEnum.NotFound: BinType = -1; break; } }