Ejemplo n.º 1
0
        private static Collection <NatureArea> GenerateNatureAreasFromShapeFile(string shapeFilePath, int epsgCode, string knr, int startFeature, int featureCount)
        {
            var natureAreas = new Collection <NatureArea>();

            shapeFilePath = FileLocator.FindFileInTree(shapeFilePath);
            Shapefile shapeFile  = Shapefile.OpenFile(shapeFilePath);
            var       endFeature = startFeature + featureCount < shapeFile.Features.Count
                ? startFeature + featureCount
                : shapeFile.Features.Count;

            for (var number = startFeature; number < endFeature; number++)
            {
                var feature = shapeFile.Features[number];
                var area    = new NatureArea {
                    UniqueId = new Identification()
                };

                var numberString = number.ToString();
                var guid         = "00000000-0000-0000-" + knr + "-";

                for (int i = 0; i < 12 - numberString.Length; ++i)
                {
                    guid += "0";
                }

                area.UniqueId.LocalId = new Guid(guid + numberString);

                area.UniqueId.NameSpace = "NBIC";
                area.UniqueId.VersionId = "1.0";

                area.Version = "1";
                area.Nivå    = Types.NatureLevel.Natursystem;

                area.Area = SqlGeometry.STGeomFromText(new SqlChars(feature.BasicGeometry.ToString()), epsgCode);

                var       natureAreaType = new NatureAreaType();
                const int rowOffset      = 0; // 1 for melhus
                natureAreaType.Code  = (string)feature.DataRow[rowOffset + 4];
                natureAreaType.Share = 1;
                if (feature.DataRow[rowOffset + 3] != DBNull.Value)
                {
                    var descriptionVariable      = new DescriptionVariable();
                    var descriptionVariableParts = ((string)feature.DataRow[rowOffset + 3]).Split('_');
                    descriptionVariable.Code  = descriptionVariableParts[0];
                    descriptionVariable.Value = descriptionVariableParts[1];
                    natureAreaType.AdditionalVariables.Add(descriptionVariable);
                }
                area.Parameters.Add(natureAreaType);

                area.Surveyed = DateTime.Parse(feature.DataRow[rowOffset + 5].ToString());
                area.Surveyer = new Contact {
                    Company = (string)feature.DataRow[rowOffset + 8]
                };
                natureAreas.Add(area);
            }
            return(natureAreas);
        }
Ejemplo n.º 2
0
        private static XElement DescriptionVariableElement(XName elementName, DescriptionVariable descriptionVariable)
        {
            var e = new NinXElement(elementName);

            if (descriptionVariable == null)
            {
                return(e);
            }

            e.Add(new NinXElement("kode", "descriptionVariable.Code", descriptionVariable.Code));
            e.Add(ContactElement("kartlegger", descriptionVariable.Surveyer));
            e.Add(new NinXElement("kartlagtDato", "descriptionVariable.Surveyed", descriptionVariable.Surveyed));
            e.Add(new NinXElement("verdi", "descriptionVariable.Value", descriptionVariable.Value));
            e.Add(new NinXElement("beskrivelse", "descriptionVariable.Description", descriptionVariable.Description));

            return(e);
        }
Ejemplo n.º 3
0
        public static async Task <List <DescriptionVariable> > Load(string connectionString, Codes descriptionVariableCodes)
        {
            Console.WriteLine("Loading DescriptionVariables");

            const string descriptionVariableSql =
                @"SELECT bv.code AS DvCode, nomt.code AS NatCode, bv.geometry_id AS NatureAreaId, nomt.fraction AS NatureAreaPercentage
                    FROM data.codes_geometry bv, data.codes_geometry nomt
                    WHERE bv.geometry_id = nomt.geometry_id and bv.code like 'NA-BS%' and nomt.code like 'NA-%' and nomt.code NOT like 'NA-BS%' and nomt.code NOT like 'NA-LKM%'
                    GROUP BY bv.code, nomt.code, bv.geometry_id, nomt.fraction";

            IEnumerable <DescriptionVariableDto> descriptionVariableDtos;

            using (var conn = new NpgsqlConnection(connectionString))
            {
                conn.Open();

                descriptionVariableDtos = await conn.QueryAsync <DescriptionVariableDto>(descriptionVariableSql);
            }

            var dict        = new Dictionary <string, DescriptionVariable>();
            var natureAreas = new Dictionary <(string, int, string), NatureAreaIdTypePercentage>();

            foreach (var dto in descriptionVariableDtos)
            {
                var codes = dto.DvCode.Split(',', StringSplitOptions.RemoveEmptyEntries);

                //dto.DvCode = dto.DvCode.Remove(0,3);

                foreach (var code in codes)
                {
                    if (!descriptionVariableCodes.Contains(dto.DvCode.ToUpper()))
                    {
                        // Ignored code as it doesn't exist in the code file.
                        continue;
                    }

                    if (!dict.TryGetValue(dto.DvCode, out var descriptionVariable))
                    {
                        descriptionVariable = new DescriptionVariable(dto.DvCode, descriptionVariableCodes.GetCode(dto.DvCode).Name);

                        dict.Add(dto.DvCode, descriptionVariable);
                    }

                    var key = (dto.DvCode, dto.NatureAreaId, dto.NatCode);
                    // descriptionVariable.NatureAreas.FirstOrDefault(na => na.NatureAreaId == dto.NatureAreaId && na.NatureAreaTypeCode == dto.NatCode);

                    if (natureAreas.TryGetValue(key, out var natureArea))
                    {
                        double newPercentage = natureArea.NatureAreaPercentage + (dto.NatureAreaPercentage / 10);

                        if (newPercentage > 1)
                        {
                            Console.WriteLine($"{key.DvCode} {key.NatCode} {key.NatureAreaId} exceeds 1 as percentage.");
                        }
                        else
                        {
                            natureArea.NatureAreaPercentage = newPercentage;
                        }
                    }
                    else
                    {
                        natureArea = new NatureAreaIdTypePercentage
                        {
                            NatureAreaId         = dto.NatureAreaId,
                            NatureAreaTypeCode   = dto.NatCode,
                            NatureAreaPercentage = dto.NatureAreaPercentage / 10
                        };

                        descriptionVariable.NatureAreas.Add(natureArea);

                        natureAreas.Add(key, natureArea);
                    }
                }
            }

            Console.WriteLine("Finished loading DescriptionVariables");

            return(dict.Values.ToList());
        }