private void TestWriteZMValuesShapeFile(bool testM)
        {
            var points = new Coordinate[3];
            points[0] = new Coordinate(0, 0);
            points[1] = new Coordinate(1, 0);
            points[2] = new Coordinate(1, 1);

            var csFactory = DotSpatialAffineCoordinateSequenceFactory.Instance;
            var sequence = csFactory.Create(3, Ordinates.XYZM);
            for (var i = 0; i < 3; i++)
            {
                sequence.SetOrdinate(i, Ordinate.X, points[i].X);
                sequence.SetOrdinate(i, Ordinate.Y, points[i].Y);
                sequence.SetOrdinate(i, Ordinate.Z, 1 + i);
                if (testM)
                    sequence.SetOrdinate(i, Ordinate.M, 11 + i);
            }
            var lineString = Factory.CreateLineString(sequence);

            var attributes = new AttributesTable();
            attributes.AddAttribute("FOO", "Trond");

            var feature = new Feature(Factory.CreateMultiLineString(new[] { lineString }), attributes);
            var features = new Feature[1];
            features[0] = feature;

            var shpWriter = new ShapefileDataWriter("ZMtest", Factory)
            {
                Header = ShapefileDataWriter.GetHeader(features[0], features.Length)
            };
            shpWriter.Write(features);

            // Now let's read the file and verify that we got Z and M back
            var factory = new GeometryFactory(DotSpatialAffineCoordinateSequenceFactory.Instance);

            using (var reader = new ShapefileDataReader("ZMtest", factory))
            {
                reader.Read();
                var geom = reader.Geometry;

                for (var i = 0; i < 3; i++)
                {
                    var c = geom.Coordinates[i];
                    Assert.AreEqual(i + 1, c.Z);
                }

                if (testM)
                {
                    sequence = ((ILineString)geom).CoordinateSequence;
                    for (var i = 0; i < 3; i++)
                    {
                        Assert.AreEqual(sequence.GetOrdinate(i, Ordinate.M), 11 + i);
                    }
                }

                // Run a simple attribute test too
                var v = reader.GetString(0);
                Assert.AreEqual(v, "Trond");
            }
        }
示例#2
0
        /// <summary>
        /// Returns MarkerType property for legend.
        /// </summary>
        /// <param name="colorRepo"></param>
        /// <param name="resultDir"></param>
        /// <param name="fi"></param>
        /// <param name="resultName"></param>
        /// <returns></returns>
        private static void ProcessVectorFile(IColorRepository colorRepo, DirectoryInfo resultDir, FileInfo fi, string resultName)
        {
            string localFileName = fi.Name;
            string localResultName = resultName;

            Console.WriteLine(string.Format("Processing {0} into {1}...", localFileName, localResultName));
            StringBuilder bldr = new StringBuilder();

            NetTopologySuite.IO.ShapefileDataReader dataReader = new NetTopologySuite.IO.ShapefileDataReader(fi.FullName, new GeometryFactory());
            NetTopologySuite.Features.FeatureCollection featureCollection = new NetTopologySuite.Features.FeatureCollection();
            List<string> csvHdr = dataReader.DbaseHeader.Fields.Select(a => a.Name).ToList();
            csvHdr.Add(appColorNamspace);
            bldr.AppendLine(string.Join(",", csvHdr)); //write csv file header
            while (dataReader.Read())
            {
                NetTopologySuite.Features.Feature feature = new NetTopologySuite.Features.Feature();
                feature.Geometry = dataReader.Geometry;

                int numFields = dataReader.DbaseHeader.NumFields + 1;
                string[] keys = new string[numFields];
                int colorValueField = -1;
                for (int i = 0; i < numFields - 1; i++)
                {
                    keys[i] = dataReader.DbaseHeader.Fields[i].Name;
                    if (keys[i].Equals(colorRepo.ColorFieldForOutput(localFileName, localResultName)))
                    {
                        colorValueField = i;
                    }
                }

                //add attributes from source attribute table
                feature.Attributes = new AttributesTable();
                List<string> csvLine = new List<string>();
                for (int i = 0; i < numFields - 1; i++)
                {
                    object val = dataReader.GetValue(i);
                    feature.Attributes.AddAttribute(keys[i], val);
                    csvLine.Add(val.ToString());
                }

                if (colorRepo.MapColorsToThisResult(localFileName, localResultName))
                {
                    //mark outline colors in a different attribute than fill colors
                    string colorNs = colorRepo.IsOutlinedNotFilled(localFileName, localResultName) ? appOutlineNamespace : appColorNamspace;
                    keys[numFields - 1] = colorNs;

                    //add additional attribute for color binding
                    string hexClr = colorRepo.SingleColorForFile(localFileName, localResultName); //only path where colorValueField, i.e. ColorMap.clrField can be unpopulated.

                    if (string.IsNullOrEmpty(hexClr) && colorValueField > -1)
                    {
                        if (colorRepo.IsCategoricalMap(localFileName, resultName))
                        {
                            //categorical color map
                             hexClr = colorRepo.ColorsOfValueInFile(localFileName, localResultName, dataReader.GetString(colorValueField)).HexColor;
                        }
                        else
                        {
                            //numerical range color map
                            hexClr = colorRepo.ColorsOfValueInFile(localFileName, localResultName, dataReader.GetDouble(colorValueField)).HexColor;
                        }
                    }

                    if (string.IsNullOrEmpty(hexClr)) // else if (string.IsNullOrEmpty(hexClr) && colorValueField < 0)
                    {
                        throw new NotSupportedException("Cannot color a file with no attributes to bind to and no single-color given.");
                    }
                    csvLine.Add(hexClr);
                    feature.Attributes.AddAttribute(colorNs, hexClr);
                }

                bldr.AppendLine(string.Join(",", csvLine));
                featureCollection.Add(feature);
            }
            GeoJsonWriter wtr = new GeoJsonWriter();
            string layerJson = wtr.Write(featureCollection);

            File.WriteAllText(resultDir.FullName + localResultName, layerJson);
            File.WriteAllText(resultDir.FullName + localResultName.Replace(".json", ".csv"), bldr.ToString());
        }