Exemplo n.º 1
0
        private static byte[] ExtractHandler(NameValueCollection boundVariables,
                                             JsonObject operationInput,
                                             string outputFormat,
                                             string requestProperties,
                                             out string responseProperties)
        {
            responseProperties = null;
            const string methodName = "ExtractIntersection";
            var          errors     = new ResponseContainer(HttpStatusCode.BadRequest, "");
            double?      featureId;

            if (!operationInput.TryGetAsDouble("id", out featureId) && featureId.HasValue)
            {
                errors.AddMessage("The id of the shape is required.");
            }

            if (errors.HasErrors)
            {
                return(Json(errors));
            }

#if !DEBUG
            _logger.LogMessage(ServerLogger.msgType.infoStandard, methodName, MessageCode, "Params received");
#endif

            var fireLayerMap = _featureClassIndexMap.First(x => x.LayerName == "Fire Perimeter");
            var fireLayer    = fireLayerMap.FeatureClass;

            var perimeterFeature = fireLayer.GetFeature(Convert.ToInt32(featureId.Value));
            var inputGeometry    = perimeterFeature.ShapeCopy;

#if !DEBUG
            _logger.LogMessage(ServerLogger.msgType.infoStandard, methodName, MessageCode, "Params valid");
#endif

            var filterGeometry = (ITopologicalOperator4)inputGeometry;
            if (filterGeometry == null)
            {
                errors.Message = "input geometry could not become a topological operator.";

                return(Json(errors));
            }

            filterGeometry.IsKnownSimple_2 = false;
            filterGeometry.Simplify();

            var totalArea = ((IArea)inputGeometry).Area;
            if (totalArea < 0)
            {
                ((ICurve)inputGeometry).ReverseOrientation();
            }

            var filter = new SpatialFilter
            {
                Geometry   = inputGeometry,
                SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
            };

            var whole = (ITopologicalOperator4)inputGeometry;
            whole.Simplify();

            var searchResults = new Dictionary <string, IList <IntersectAttributes> >
            {
                {
                    "fire", new[]
                    {
                        new IntersectAttributes(new[]
                        {
                            new KeyValuePair <string, object>("total", "total")
                        })
                        {
                            Intersect = totalArea
                        }
                    }
                }
            };

            var criterias = new List <Criteria>
            {
                new Criteria
                {
                    LayerIndex         = _featureClassIndexMap.First(x => x.LayerName == "Municipal Boundary").Index,
                    Attributes         = new[] { "NAME" },
                    JsonPropertyName   = "muni",
                    CalculationCommand = new CalculateIntersectionCommand(whole, _logger)
                },
                new Criteria
                {
                    LayerIndex           = _featureClassIndexMap.First(x => x.LayerName == "Municipal Boundary").Index,
                    FeatureClassIndexMap = _featureClassIndexMap,
                    Attributes           = new[] { "NAME" },
                    JsonPropertyName     = "muniPrivate",
                    CalculationCommand   = new CalculateMuniPrivateCommand(whole, _logger)
                    {
                        LandOwnership = _featureClassIndexMap.Single(x => x.LayerName == "Land Ownership")
                    }
                },
                new Criteria
                {
                    LayerIndex         = _featureClassIndexMap.First(x => x.LayerName == "County Boundary").Index,
                    Attributes         = new[] { "NAME" },
                    JsonPropertyName   = "county",
                    CalculationCommand = new CalculateIntersectionCommand(whole, _logger)
                },
                new Criteria
                {
                    LayerIndex         = _featureClassIndexMap.First(x => x.LayerName == "County Boundary").Index,
                    Attributes         = new[] { "NAME" },
                    JsonPropertyName   = "countyPrivate",
                    CalculationCommand = new CalculateCountyPrivateCommand(whole, _logger)
                    {
                        LandOwnership  = _featureClassIndexMap.Single(x => x.LayerName == "Land Ownership"),
                        Municipalities = _featureClassIndexMap.First(x => x.LayerName == "Municipal Boundary")
                    }
                },
                new Criteria
                {
                    LayerIndex         = _featureClassIndexMap.Single(x => x.LayerName == "Land Ownership").Index,
                    Attributes         = new[] { "OWNER" },
                    JsonPropertyName   = "owner",
                    CalculationCommand = new CalculateIntersectionCommand(whole, _logger)
                },
                new Criteria
                {
                    LayerIndex         = _featureClassIndexMap.Single(x => x.LayerName == "Land Ownership").Index,
                    Attributes         = new[] { "ADMIN" },
                    JsonPropertyName   = "admin",
                    CalculationCommand = new CalculateIntersectionCommand(whole, _logger)
                },
                new Criteria
                {
                    LayerIndex         = _featureClassIndexMap.Single(x => x.LayerName == "States Boundary").Index,
                    Attributes         = new[] { "STATE_NAME" },
                    JsonPropertyName   = "state",
                    CalculationCommand = new CalculateIntersectionCommand(whole, _logger)
                }
            };

            foreach (var criteria in criterias)
            {
                // get the IFeatureClass
                var container = _featureClassIndexMap.Single(x => x.Index == criteria.LayerIndex);
                // get the index of the fields to calculate intersections for
                var fieldMap = container.FieldMap.Select(x => x.Value)
                               .Where(y => criteria.Attributes.Contains(y.Field.ToUpper()))
                               .ToList();
#if !DEBUG
                _logger.LogMessage(ServerLogger.msgType.infoStandard, methodName, MessageCode,
                                   string.Format("Querying {0} at index {1}", container.LayerName, container.Index));
#endif
                var      cursor = container.FeatureClass.Search(filter, true);
                IFeature feature;
                while ((feature = cursor.NextFeature()) != null)
                {
                    var values     = new GetValueAtIndexCommand(fieldMap, feature).Execute();
                    var attributes = new IntersectAttributes(values);
#if !DEBUG
                    _logger.LogMessage(ServerLogger.msgType.infoStandard, methodName, MessageCode,
                                       "intersecting " + container.LayerName);
#endif
                    IntersectionPart intersectionPart;
                    try
                    {
                        intersectionPart = criteria.GetIntersectionWith(feature);
                    }
                    catch (Exception ex)
                    {
                        return(Json(new ResponseContainer(HttpStatusCode.InternalServerError, ex.Message)));
                    }

                    attributes.Intersect = intersectionPart.Size;

                    if (searchResults.ContainsKey(criteria.JsonPropertyName))
                    {
                        if (searchResults[criteria.JsonPropertyName].Any(x => new MultiSetComparer <object>().Equals(x.Attributes, attributes.Attributes)))
                        {
                            var duplicate = searchResults[criteria.JsonPropertyName]
                                            .Single(x => new MultiSetComparer <object>().Equals(x.Attributes, attributes.Attributes));

                            duplicate.Intersect += attributes.Intersect;
                        }
                        else
                        {
                            searchResults[criteria.JsonPropertyName].Add(attributes);
                        }
                    }
                    else
                    {
                        searchResults[criteria.JsonPropertyName] = new Collection <IntersectAttributes> {
                            attributes
                        };
                    }
                }
            }

            var response = new IntersectResult(searchResults);

#if !DEBUG
            _logger.LogMessage(ServerLogger.msgType.infoStandard, methodName, MessageCode, string.Format("Returning results {0}", searchResults.Count));
#endif

            return(Json(new ResponseContainer <IntersectResult>(response)));
        }