示例#1
0
        public void TestPointLine2()
        {
            // test when the point being tested in a node on the line
            Coordinate coordLine1  = new Coordinate(5.0, 5.0);
            Coordinate coordLine2  = new Coordinate(10.0, 10.0);
            Coordinate coordPoint1 = new Coordinate(6.0, 6.0);

            Coordinates coordinates = new Coordinates();

            coordinates.Add(coordLine1);
            coordinates.Add(coordLine2);

            Point point1 = _factory.CreatePoint(coordPoint1);

            LineString linestring = _factory.CreateLineString(coordinates);

            Assertion.AssertEquals("intersects", true, point1.Intersects(linestring));
            Assertion.AssertEquals("intersects", true, linestring.Intersects(point1));
            Assertion.AssertEquals("disjoint", false, point1.Disjoint(linestring));
            Assertion.AssertEquals("contains", false, point1.Contains(linestring));
            Assertion.AssertEquals("OppositeContains", true, linestring.Contains(point1));
            Assertion.AssertEquals("within", true, point1.Within(linestring));                          // point1 is within linestring and linestring contains point.

            // always returns false when a point is involves
            Assertion.AssertEquals("crosses", false, point1.Crosses(linestring));

            Assertion.AssertEquals("touches", false, point1.Touches(linestring));                       // false because point is in the interior of linestring and not the boundary. The boundary is the endpoints.

            // always returns false when a point is involved
            Assertion.AssertEquals("overlaps", false, point1.Overlaps(linestring));
        }
        public void test_Geometry()
        {
            LineString[] linestrings = new LineString[2];
            Coordinates  coords1     = new Coordinates();
            Coordinate   coord       = new Coordinate(5, 3);

            coords1.Add(coord);
            coord = new Coordinate(4, 5);
            coords1.Add(coord);
            coord = new Coordinate(3, 4);
            coords1.Add(coord);

            GeometryFactory gf = new GeometryFactory(_precMod, _sRID);
            LineString      ls = gf.CreateLineString(coords1);

            linestrings[0] = ls;

            Coordinates coords2 = new Coordinates();

            coord = new Coordinate(2, 7);
            coords2.Add(coord);
            coord = new Coordinate(9, 2);
            coords2.Add(coord);
            coord = new Coordinate(7, 9);
            coords2.Add(coord);

            ls             = gf.CreateLineString(coords2);
            linestrings[1] = ls;

            MultiLineString mls = gf.CreateMultiLineString(linestrings);

            Assertion.AssertEquals("Geometry-1: ", "LineString:(5, 3, NaN),(4, 5, NaN),(3, 4, NaN)", mls.GetGeometryN(0).ToString());
            Assertion.AssertEquals("Geometry-2: ", "LineString:(2, 7, NaN),(9, 2, NaN),(7, 9, NaN)", mls.GetGeometryN(1).ToString());
        }
示例#3
0
        public void TestPointLine1()
        {
            // test when the point being tested in a node on the line
            Coordinate coordLine1  = new Coordinate(5.0, 5.0);
            Coordinate coordLine2  = new Coordinate(10.0, 10.0);
            Coordinate coordPoint1 = new Coordinate(5.0, 5.0);

            Coordinates coordinates = new Coordinates();

            coordinates.Add(coordLine1);
            coordinates.Add(coordLine2);

            Point point1 = _factory.CreatePoint(coordPoint1);

            LineString linestring = _factory.CreateLineString(coordinates);

            Assertion.AssertEquals("intersects", true, point1.Intersects(linestring));
            Assertion.AssertEquals("disjoint", false, point1.Disjoint(linestring));
            Assertion.AssertEquals("contains", false, point1.Contains(linestring));
            Assertion.AssertEquals("within", false, point1.Within(linestring));

            // always returns false when a point is involves
            Assertion.AssertEquals("crosses", false, point1.Crosses(linestring));

            Assertion.AssertEquals("touches", true, point1.Touches(linestring));

            // always returns false when a point is involved
            Assertion.AssertEquals("overlaps", false, point1.Overlaps(linestring));
        }
示例#4
0
        }         // public void AddSplitEdges(ArrayList edgeList)

        /// <summary>
        /// Create a new "split edge" with the section of points between
        /// (and including) the two intersections.
        /// The label for the new edge is the same as the label for the parent edge.
        /// </summary>
        private Edge CreateSplitEdge(EdgeIntersection ei0, EdgeIntersection ei1)
        {
            //Trace.WriteLine("\ncreateSplitEdge"); Trace.WriteLine(ei0.ToString()); Trace.WriteLine(ei1.ToString());
            int npts = ei1.SegmentIndex - ei0.SegmentIndex + 2;

            Coordinate lastSegStartPt = _edge.Coordinates[ei1.SegmentIndex];
            // if the last intersection point is not equal to the its segment start pt,
            // add it to the points list as well.
            // (This check is needed because the distance metric is not totally reliable!)
            bool useIntPt1 = ei1.Distance > 0.0 || !ei1.Coordinate.Equals(lastSegStartPt);

            Coordinates coordinatePts = new Coordinates();              // new coordinates collection

            coordinatePts.Add(new Coordinate(ei0.Coordinate));
            for (int i = ei0.SegmentIndex + 1; i <= ei1.SegmentIndex; i++)
            {
                coordinatePts.Add(_edge.Coordinates[i]);
            }

            if (useIntPt1)
            {
                coordinatePts.Add(ei1.Coordinate);
            }

            return(new Edge(coordinatePts, new Label(_edge.Label)));
        }         // Edge CreateSplitEdge(EdgeIntersection ei0, EdgeIntersection ei1)
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="original">
        ///		The vertices of a linear ring, which may or may not be flattened (i.e. vertices collinear)
        /// </param>
        /// <returns>
        ///		The coordinates with unnecessary (collinear) vertices removed
        /// </returns>
        private Coordinates CleanRing(Coordinates original)
        {
            if (!(original[0] == original[original.Count - 1]))
            {
                throw new InvalidProgramException("Start and end points of ring are not the same.");
            }
            Coordinates cleanedRing = new Coordinates();
            Coordinate  previousDistinctCoordinate = null;

            for (int i = 0; i <= original.Count - 2; i++)
            {
                Coordinate currentCoordinate = original[i];
                Coordinate nextCoordinate    = original[i + 1];
                if (currentCoordinate.Equals(nextCoordinate))
                {
                    continue;
                }
                if (previousDistinctCoordinate != null &&
                    IsBetween(previousDistinctCoordinate, currentCoordinate, nextCoordinate))
                {
                    continue;
                }
                cleanedRing.Add(currentCoordinate);
                previousDistinctCoordinate = currentCoordinate;
            }
            cleanedRing.Add(original[original.Count - 1]);
            //Coordinates cleanedRingCoordinates = new Coordinate[cleanedRing.Count];
            //return (Coordinates) cleanedRing.toArray(cleanedRingCoordinates);
            return(cleanedRing);
        }
示例#6
0
        public void test_ToString()
        {
            Coordinates coords = new Coordinates();
            Coordinate  coord  = new Coordinate(1.0, 2.0);

            coords.Add(coord);
            coord = new Coordinate(3.0, 4.0);
            coords.Add(coord);

            GeometryFactory gf  = new GeometryFactory(_precMod, _sRID);
            LineString      ls1 = gf.CreateLineString(coords);

            Assertion.AssertEquals("ToString-1: ", "LineString:(1, 2, NaN),(3, 4, NaN)", ls1.ToString());

            coord = new Coordinate(2.4, 4.9);
            coords.Add(coord);
            LineString ls2 = gf.CreateLineString(coords);

            Assertion.AssertEquals("ToString-2: ", "LineString:(1, 2, NaN),(3, 4, NaN),(2.4, 4.9, NaN)", ls2.ToString());

            coord = new Coordinate(1.0, 1.0);
            coords.Add(coord);
            LineString ls3 = gf.CreateLineString(coords);

            Assertion.AssertEquals("ToString-3: ", "LineString:(1, 2, NaN),(3, 4, NaN),(2.4, 4.9, NaN),(1, 1, NaN)", ls3.ToString());
        }
示例#7
0
        private void AddPt(Coordinate pt)
        {
            Coordinate bufPt = new Coordinate(pt);

            if (_makePrecise)
            {
                bufPt.MakePrecise();
            }
            // don't add duplicate points
            Coordinate lastPt = null;

            if (_ptList.Count >= 1)
            {
                lastPt = (Coordinate)_ptList[_ptList.Count - 1];
            }
            if (lastPt != null && bufPt.Equals(lastPt))
            {
                return;
            }

            // if new segment is shorter than tolerance length, skip it
            if (_useMinSegmentLength)
            {
                if (lastPt != null && bufPt.Distance(lastPt) < _minSegmentLength)
                {
                    return;
                }
            }

            _ptList.Add(bufPt);
            //System.out.println(bufPt);
        }
        void AddHalfFieldPosTagger(Play play, bool fill)
        {
            List <Coordinates> coords = new List <Coordinates>();

            if (play.HalfFieldPosition != null)
            {
                coords.Add(play.HalfFieldPosition);
            }
            else if (fill)
            {
                Coordinates c = new Coordinates();
                c.Add(new Point((int)(hFieldPixbuf.Width * 0.25),
                                (int)(hFieldPixbuf.Height * 0.25)));
                if (play.Category.HalfFieldPositionIsDistance)
                {
                    c.Add(new Point((int)(hFieldPixbuf.Width * 0.75),
                                    (int)(hFieldPixbuf.Height * 0.75)));
                }
                coords.Add(c);
                play.HalfFieldPosition = c;
            }
            else
            {
                return;
            }
            hfield.Coordinates = coords;
            hfield.Visible     = true;
        }
示例#9
0
        /// <summary>
        /// Returns the next array of Coordinates in the stream.
        /// </summary>
        /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text format.  The
        /// next element returned by the stream should be "(" (the beginning of "(x1 y1, x2 y2, ..., xn yn)" or
        /// "EMPTY".</param>
        /// <returns>The next array of Coordinates in the stream, or an empty array of "EMPTY" is the
        /// next element returned by the stream.</returns>
        private Coordinates GetCoordinates(WktStreamTokenizer tokenizer)
        {
            Coordinates coordinates = new Coordinates();
            string      nextToken   = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(coordinates);
            }
            Coordinate externalCoordinate = new Coordinate();
            Coordinate internalCoordinate = new Coordinate();

            externalCoordinate.X = GetNextNumber(tokenizer);
            externalCoordinate.Y = GetNextNumber(tokenizer);
            _precisionModel.ToInternal(externalCoordinate, internalCoordinate);
            coordinates.Add(internalCoordinate);
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken == ",")
            {
                externalCoordinate.X = this.GetNextNumber(tokenizer);
                externalCoordinate.Y = this.GetNextNumber(tokenizer);
                internalCoordinate   = new Coordinate();
                _precisionModel.ToInternal(externalCoordinate, internalCoordinate);
                coordinates.Add(internalCoordinate);
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(coordinates);
            //Coordinate[] array = new Coordinate[coordinates.Count];
            //return (Coordinate[]) coordinates.ToArray(typeof(Coordinate));
        }
示例#10
0
        }         // protected void MergeLabel( Label deLabel, int geomIndex )

        /// <summary>
        /// Adds the edges coordinates to this EdgeRings coordinates.
        /// </summary>
        /// <param name="edge"></param>
        /// <param name="isForward"></param>
        /// <param name="isFirstEdge"></param>
        protected void AddPoints(Edge edge, bool isForward, bool isFirstEdge)
        {
            Coordinates edgePts = edge.Coordinates;

            if (isForward)
            {
                int startIndex = 1;
                if (isFirstEdge)
                {
                    startIndex = 0;
                }
                for (int i = startIndex; i < edgePts.Count; i++)
                {
                    _pts.Add(edgePts[i]);
                }
            }             // if ( isForward )
            else
            {             // is backward
                int startIndex = edgePts.Count - 2;
                if (isFirstEdge)
                {
                    startIndex = edgePts.Count - 1;
                }
                for (int i = startIndex; i >= 0; i--)
                {
                    _pts.Add(edgePts[i]);
                }
            }     // else
        }         // protected void AddPoints( Edge edge, bool isForward, bool isFirstEdge )
示例#11
0
        /// <summary>
        /// Returns the collapsed edge.
        /// </summary>
        /// <returns>Returns the collapsed edge.</returns>
        public Edge GetCollapsedEdge()
        {
            Coordinates newPts = new Coordinates();

            newPts.Add(_pts[0]);
            newPts.Add(_pts[1]);
            Edge newEdge = new Edge(newPts, Label.ToLineLabel(_label));

            return(newEdge);
        }         // public Edge GetCollapsedEdge()
示例#12
0
        private Polygon Poly1()
        {
            _coords1 = new Coordinates();
            Coordinate coord = new Coordinate(5, 1);

            _coords1.Add(coord);
            coord = new Coordinate(6, 2);
            _coords1.Add(coord);
            coord = new Coordinate(7, 3);
            _coords1.Add(coord);
            coord = new Coordinate(6, 4);
            _coords1.Add(coord);
            coord = new Coordinate(5, 5);
            _coords1.Add(coord);
            coord = new Coordinate(4, 4);
            _coords1.Add(coord);
            coord = new Coordinate(3, 3);
            _coords1.Add(coord);
            coord = new Coordinate(4, 2);
            _coords1.Add(coord);
            coord = new Coordinate(5, 1);
            _coords1.Add(coord);

            _gf        = new GeometryFactory(_precMod, _sRID);
            _exterior1 = _gf.CreateLinearRing(_coords1);
            Polygon polygon = _gf.CreatePolygon(_exterior1);

            return(polygon);
        }
示例#13
0
        public void TestLineString1()
        {
            Coordinates coordinates = new Coordinates();

            coordinates.Add(new Coordinate(1, 2));
            coordinates.Add(new Coordinate(1.2, 2.3));
            LineString linestring = _factory.CreateLineString(coordinates);
            string     wkt        = _writer.WriteFormatted(linestring);

            Assertion.AssertEquals("multi point", "LINESTRING (1 2, 1.2 2.3)", wkt);
        }
示例#14
0
        public void TestMultiPoint1()
        {
            Coordinates coordinates = new Coordinates();

            coordinates.Add(new Coordinate(1, 2));
            coordinates.Add(new Coordinate(1.2, 2.3));
            MultiPoint multipoint = _factory.CreateMultiPoint(coordinates);
            string     wkt        = _writer.WriteFormatted(multipoint);

            Assertion.AssertEquals("multi point", "MULTIPOINT (1 2, 1.2 2.3)", wkt);
        }
示例#15
0
        public void test_ToString()
        {
            Coordinates coords = new Coordinates();
            Coordinate  coord  = new Coordinate(2.0, 4.1);

            coords.Add(coord);
            coord = new Coordinate(6.2, 8.4);
            coords.Add(coord);

            Assertion.AssertEquals("ToString-1: ", "(2, 4.1, NaN),(6.2, 8.4, NaN)", coords.ToString());
        }
示例#16
0
        public void test_Envelope()
        {
            Polygon polygon = Poly1();

            Coordinates coords = new Coordinates();
            Coordinate  coord  = new Coordinate(3.0, 1.0);

            coords.Add(coord);
            coord = new Coordinate(7.0, 1.0);
            coords.Add(coord);
            coord = new Coordinate(7.0, 5.0);
            coords.Add(coord);
            coord = new Coordinate(3.0, 5.0);
            coords.Add(coord);
            coord = new Coordinate(3.0, 1.0);
            coords.Add(coord);

            Geometry    env     = polygon.GetEnvelope() as Geometry;
            Coordinates coords2 = env.GetCoordinates();

            Assertion.AssertEquals("Envelope-1: ", coords[0], coords2[0]);
            Assertion.AssertEquals("Envelope-2: ", coords[1], coords2[1]);
            Assertion.AssertEquals("Envelope-3: ", coords[2], coords2[2]);
            Assertion.AssertEquals("Envelope-4: ", coords[3], coords2[3]);
            Assertion.AssertEquals("Envelope-5: ", coords[4], coords2[4]);

            polygon = Poly2();

            coords = new Coordinates();
            coord  = new Coordinate(7.0, 13.0);
            coords.Add(coord);
            coord = new Coordinate(15.0, 13.0);
            coords.Add(coord);
            coord = new Coordinate(15.0, 21.0);
            coords.Add(coord);
            coord = new Coordinate(7.0, 21.0);
            coords.Add(coord);
            coord = new Coordinate(7.0, 13.0);
            coords.Add(coord);

            env     = polygon.GetEnvelope() as Geometry;
            coords2 = env.GetCoordinates();
            Assertion.AssertEquals("Envelope-6: ", coords[0], coords2[0]);
            Assertion.AssertEquals("Envelope-7: ", coords[1], coords2[1]);
            Assertion.AssertEquals("Envelope-8: ", coords[2], coords2[2]);
            Assertion.AssertEquals("Envelope-9: ", coords[3], coords2[3]);
            Assertion.AssertEquals("Envelope-10: ", coords[4], coords2[4]);
        }
示例#17
0
        public void AddCorrectlyNegativeValues()
        {
            Coordinates coordinates = new Coordinates(0, 5);
            Coordinates toAdd       = new Coordinates(1, -3);

            Assert.Equal(new Coordinates(1, 2), coordinates.Add(toAdd));
        }
示例#18
0
        public void Test_AddMethod()
        {
            Coordinates bm  = new Coordinates();
            double      res = bm.Add(10, 10);

            Assert.AreEqual(res, 20);
        }
        public void SetCoordinates(IBoard board)
        {
            if (!ValidateShipFit(board))
            {
                throw new ArgumentException("Ship does not fit on board");
            }
            ;

            var reverseDirection = false;

            if (ShipDirection == ShipDirection.Horizontal && StartingCoordinate.X + ShipLength > board.Width)
            {
                reverseDirection = true;
            }

            if (ShipDirection == ShipDirection.Vertical && StartingCoordinate.Y + ShipLength > board.Lenght)
            {
                reverseDirection = true;
            }

            Coordinates.Add(StartingCoordinate);

            for (int i = 1; i < ShipLength; i++)
            {
                var increment = reverseDirection ? -i : i;

                var x = ShipDirection == ShipDirection.Horizontal ? StartingCoordinate.X + increment : StartingCoordinate.X;
                var y = ShipDirection == ShipDirection.Vertical ? StartingCoordinate.Y + increment : StartingCoordinate.Y;

                Coordinates.Add(new Coordinate(x, y));
            }
        }
示例#20
0
        public void AddCorrectlyZero()
        {
            Coordinates coordinates = new Coordinates(0, 5);
            Coordinates toAdd       = new Coordinates(0, -3);

            Assert.Equal(new Coordinates(0, 2), coordinates.Add(toAdd));
        }
示例#21
0
        private void PictureBox_Click(object sender, MouseEventArgs e)
        {
            var point = new Point(e.X, e.Y);

            Coordinates.Add(point);
            MessageBox.Show(string.Format("You've selected a pixel with coordinates: {0}:{1}", point.X, point.Y));
        }
        private GeometryCollection CreateCollection2()
        {
            //create a new geometries array
            Geometry[] geom = new Geometry[10];

            Coordinate  coordinate = new Coordinate();
            Coordinates coords     = new Coordinates();

            GeometryFactory gf         = new GeometryFactory(_precMod, _sRID);
            LineString      lineString = gf.CreateLineString(coords);

            for (int c = 0; c < 10; c++)
            {
                for (int i = c; i < c + 10; i++)
                {
                    //if this isn't here the coordinates for all the points are reset every time the coordinate is reset
                    coordinate = new Coordinate();
                    //make the x coordinate equal to the iterator
                    coordinate.X = (double)i;
                    //make the y coordinate equal to the iterator plus 10
                    coordinate.Y = (double)i + 10;
                    coords.Add(coordinate);
                }
                lineString = gf.CreateLineString(coords);
                geom[c]    = lineString;
            }
            //put the geometries into a geometry collection
            GeometryCollection geoColl = gf.CreateGeometryCollection(geom);

            return(geoColl);
        }
示例#23
0
 private void OnOgrGeometryChanged()
 {
     _ignoreGeoChanged   = true;
     _ignoreCoordChanged = true;
     Geometries.Clear();
     Coordinates.Clear();
     if (OgrGeometry != null)
     {
         var geometryCount = OgrGeometry.GetGeometryCount();
         if (geometryCount == 0)
         {
             var pointCount = OgrGeometry.GetPointCount();
             var dimension  = OgrGeometry.GetCoordinateDimension();
             for (int i = 0; i < pointCount; i++)
             {
                 double[] argout = new double[dimension];
                 OgrGeometry.GetPoint(i, argout);
                 Coordinate coordinate = new Coordinate(argout);
                 Coordinates.Add(coordinate);
             }
         }
         else
         {
             for (int i = 0; i < geometryCount; i++)
             {
                 var      ogrGeometry = OgrGeometry.GetGeometryRef(i);
                 Geometry geometry    = new Geometry(ogrGeometry);
                 Geometries.Add(geometry);
             }
         }
     }
     _ignoreGeoChanged   = false;
     _ignoreCoordChanged = false;
 }
 public void SetXYZ(double x, double y, double z)
 {
     Coordinates.Clear();
     Coordinates.Add(x);
     Coordinates.Add(y);
     Coordinates.Add(z);
 }
示例#25
0
 public SamplePoint(List <double> coordinates) : base(coordinates)
 {
     if (!coordinates.Any())
     {
         Coordinates.Add(0.0);
     }
 }
示例#26
0
 public SamplePoint(params double[] coordinates) : base(coordinates.ToList())
 {
     if (!coordinates.Any())
     {
         Coordinates.Add(0.0);
     }
 }
示例#27
0
 public Region(RegionInfo regionInfo)
 {
     foreach (var pos in regionInfo.Coordinates)
     {
         Coordinates.Add(new Coordinate(pos));
     }
 }
示例#28
0
    public override string Part1(List <string> input, bool isTestRun)
    {
        Coordinates       = new();
        MapHeight         = input.Count;
        MapWith           = input[0].Length;
        Coordinate[,] map = new Coordinate[MapWith, MapHeight];
        for (int y = 0; y < MapHeight; y++)
        {
            for (int x = 0; x < MapWith; x++)
            {
                Coordinate coord = new Coordinate(int.Parse(input[y][x].ToString()), x, y, ref map);
                Coordinates.Add(coord);
            }
        }


        //Dijkstra’s Shortest Path Algorithm https://www.youtube.com/watch?v=pVfj6mxhdMw
        //Coordinate start = Coordinates.Where(c => c.X == 0 && c.Y == 0).Single();
        Coordinate start = Coordinates.Where(c => c.X == 0 && c.Y == 0).Single();

        start.Cost = 0;
        Target     = start.Map[MapWith - 1, MapHeight - 1];
        Dijkstra();
        WriteHtml($"Part1_Testrun{(isTestRun)}");

        return($"{Target.Cost}");
    }
示例#29
0
        private MultiLineString CreateMLS()
        {
            Coordinates     coords     = new Coordinates();
            Coordinate      coord      = new Coordinate();
            GeometryFactory gf         = new GeometryFactory(_precMod, _sRID);
            LineString      lineString = gf.CreateLineString(coords);

            LineString[] ls = new LineString[10];
            for (int i = 0; i < 10; i++)
            {
                coords = new Coordinates();
                for (int j = i; j < i + 10; j++)
                {
                    coord   = new Coordinate();
                    coord.X = (double)j;
                    coord.Y = (double)j + 5;
                    coords.Add(coord);
                }
                lineString = gf.CreateLineString(coords);
                ls[i]      = lineString;
            }
            MultiLineString multiLS = gf.CreateMultiLineString(ls);

            return(multiLS);
        }
示例#30
0
        public void test_Add()
        {
            Coordinates coords = Coords1();
            Coordinate  coord  = new Coordinate(4.0, 2.0);

            coords.Add(coord);
            Assertion.AssertEquals("Add-1: ", 11, coords.Count);
            Assertion.AssertEquals("Add-2: ", 4.0, coords[10].X);
            Assertion.AssertEquals("Add-3: ", 2.0, coords[10].Y);

            coord = new Coordinate();
            coords.Add(coord);
            Assertion.AssertEquals("Add-4: ", 12, coords.Count);
            //Zeros are added for null coordinates
            Assertion.AssertEquals("Add-5: ", 0.0, coords[11].X);
            Assertion.AssertEquals("Add-6: ", 0.0, coords[11].Y);
        }
示例#31
0
        private void writeKML(string filename)
        {
            try
            {
                writeGPX(filename);
            }
            catch { }

            Color[] colours = { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet, Color.Pink };

            AltitudeMode altmode = AltitudeMode.absolute;

            if (MainV2.cs.firmware == MainV2.Firmwares.ArduCopter2)
            {
                altmode = AltitudeMode.relativeToGround; // because of sonar, this is both right and wrong. right for sonar, wrong in terms of gps as the land slopes off.
            }

            KMLRoot kml = new KMLRoot();
            Folder fldr = new Folder("Log");

            Style style = new Style();
            style.Id = "yellowLineGreenPoly";
            style.Add(new LineStyle(HexStringToColor("7f00ffff"), 4));

            PolyStyle pstyle = new PolyStyle();
            pstyle.Color = HexStringToColor("7f00ff00");
            style.Add(pstyle);

            kml.Document.AddStyle(style);

            int stylecode = 0xff;
            int g = -1;
            foreach (List<Point3D> poslist in position)
            {
                g++;
                if (poslist == null)
                    continue;

                LineString ls = new LineString();
                ls.AltitudeMode = altmode;
                ls.Extrude = true;
                //ls.Tessellate = true;

                Coordinates coords = new Coordinates();
                coords.AddRange(poslist);

                ls.coordinates = coords;

                Placemark pm = new Placemark();

                string mode = "";
                if (g < modelist.Count)
                    mode = modelist[g];

                pm.name = g + " Flight Path " + mode;
                pm.styleUrl = "#yellowLineGreenPoly";
                pm.LineString = ls;

                stylecode = colours[g % (colours.Length - 1)].ToArgb();

                Style style2 = new Style();
                Color color = Color.FromArgb(0xff, (stylecode >> 16) & 0xff, (stylecode >> 8) & 0xff, (stylecode >> 0) & 0xff);
                log.Info("colour " + color.ToArgb().ToString("X") + " " + color.ToKnownColor().ToString());
                style2.Add(new LineStyle(color, 4));

                pm.AddStyle(style2);

                fldr.Add(pm);
            }

            Folder planes = new Folder();
            planes.name = "Planes";
            fldr.Add(planes);

            Folder waypoints = new Folder();
            waypoints.name = "Waypoints";
            fldr.Add(waypoints);

            LineString lswp = new LineString();
            lswp.AltitudeMode = AltitudeMode.relativeToGround;
            lswp.Extrude = true;

            Coordinates coordswp = new Coordinates();

            foreach (PointLatLngAlt p1 in cmd)
            {
                coordswp.Add(new Point3D(p1.Lng, p1.Lat, p1.Alt));
            }

            lswp.coordinates = coordswp;

            Placemark pmwp = new Placemark();

            pmwp.name = "Waypoints";
            //pm.styleUrl = "#yellowLineGreenPoly";
            pmwp.LineString = lswp;

            waypoints.Add(pmwp);

            int a = 0;
            int l = -1;

            Model lastmodel = null;

            foreach (Data mod in flightdata)
            {
                l++;
                if (mod.model.Location.latitude == 0)
                    continue;

                if (lastmodel != null)
                {
                    if (lastmodel.Location.Equals(mod.model.Location))
                    {
                        continue;
                    }
                }
                Placemark pmplane = new Placemark();
                pmplane.name = "Plane " + a;

                pmplane.visibility = false;

                Model model = mod.model;
                model.AltitudeMode = altmode;
                model.Scale.x = 2;
                model.Scale.y = 2;
                model.Scale.z = 2;

                try
                {
                    pmplane.description = @"<![CDATA[
              <table>
                <tr><td>Roll: " + model.Orientation.roll + @" </td></tr>
                <tr><td>Pitch: " + model.Orientation.tilt + @" </td></tr>
                <tr><td>Yaw: " + model.Orientation.heading + @" </td></tr>
                <tr><td>WP dist " + mod.ntun[2] + @" </td></tr>
                <tr><td>tar bear " + mod.ntun[3] + @" </td></tr>
                <tr><td>nav bear " + mod.ntun[4] + @" </td></tr>
                <tr><td>alt error " + mod.ntun[5] + @" </td></tr>
              </table>
            ]]>";
                }
                catch { }

                try
                {

                    pmplane.Point = new KmlPoint((float)model.Location.longitude, (float)model.Location.latitude, (float)model.Location.altitude);
                    pmplane.Point.AltitudeMode = altmode;

                    Link link = new Link();
                    link.href = "block_plane_0.dae";

                    model.Link = link;

                    pmplane.Model = model;

                    planes.Add(pmplane);
                }
                catch { } // bad lat long value

                lastmodel = mod.model;

                a++;
            }

            kml.Document.Add(fldr);

            kml.Save(filename);

            // create kmz - aka zip file

            FileStream fs = File.Open(filename.Replace(".log.kml", ".kmz"), FileMode.Create);
            ZipOutputStream zipStream = new ZipOutputStream(fs);
            zipStream.SetLevel(9); //0-9, 9 being the highest level of compression
            zipStream.UseZip64 = UseZip64.Off; // older zipfile

            // entry 1
            string entryName = ZipEntry.CleanName(Path.GetFileName(filename)); // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();

            File.Delete(filename);

            filename = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "block_plane_0.dae";

            // entry 2
            entryName = ZipEntry.CleanName(Path.GetFileName(filename)); // Removes drive from name and fixes slash direction
            newEntry = new ZipEntry(entryName);
            newEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();

            zipStream.IsStreamOwner = true;	// Makes the Close also Close the underlying stream
            zipStream.Close();

            positionindex = 0;
            modelist.Clear();
            flightdata.Clear();
            position = new List<Core.Geometry.Point3D>[200];
            cmd.Clear();
        }
示例#32
0
        public void writeKML(string filename)
        {
            try
            {
                writeGPX(filename);

                writeRinex(filename);
            }
            catch
            {
            }

            Color[] colours =
            {
                Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo,
                Color.Violet, Color.Pink
            };

            AltitudeMode altmode = AltitudeMode.absolute;

            KMLRoot kml = new KMLRoot();
            Folder fldr = new Folder("Log");

            Style style = new Style();
            style.Id = "yellowLineGreenPoly";
            style.Add(new LineStyle(HexStringToColor("7f00ffff"), 4));

            Style style1 = new Style();
            style1.Id = "spray";
            style1.Add(new LineStyle(HexStringToColor("4c0000ff"), 0));
            style1.Add(new PolyStyle() {Color = HexStringToColor("4c0000ff")});

            PolyStyle pstyle = new PolyStyle();
            pstyle.Color = HexStringToColor("7f00ff00");
            style.Add(pstyle);

            kml.Document.AddStyle(style);
            kml.Document.AddStyle(style1);

            int stylecode = 0xff;
            int g = -1;
            foreach (List<Point3D> poslist in position)
            {
                g++;
                if (poslist == null)
                    continue;

                /*
                List<PointLatLngAlt> pllalist = new List<PointLatLngAlt>();

                foreach (var point in poslist)
                {
                    pllalist.Add(new PointLatLngAlt(point.Y, point.X, point.Z, ""));
                }

                var ans = Utilities.LineOffset.GetPolygon(pllalist, 2);



                while (ans.Count > 0)
                {
                    var first = ans[0];
                    var second = ans[1];
                    var secondlast = ans[ans.Count - 2];
                    var last = ans[ans.Count-1];

                    ans.Remove(first);
                    ans.Remove(last);

                    var polycoords = new BoundaryIs();

                    polycoords.LinearRing = new LinearRing();

                    polycoords.LinearRing.Coordinates.Add(new Point3D(first.Lng, first.Lat, 1));
                    polycoords.LinearRing.Coordinates.Add(new Point3D(second.Lng, second.Lat, 1));
                    polycoords.LinearRing.Coordinates.Add(new Point3D(secondlast.Lng, secondlast.Lat, 1));
                    polycoords.LinearRing.Coordinates.Add(new Point3D(last.Lng, last.Lat, 1));
                    polycoords.LinearRing.Coordinates.Add(new Point3D(first.Lng, first.Lat, 1));

                    //if (!IsClockwise(polycoords.LinearRing.Coordinates))
                      //  polycoords.LinearRing.Coordinates.Reverse();

                    Polygon kmlpoly = new Polygon() { AltitudeMode = AltitudeMode.relativeToGround, Extrude = false, OuterBoundaryIs = polycoords };

                    Placemark pmpoly = new Placemark();
                    pmpoly.Polygon = kmlpoly;
                    pmpoly.name = g + " test";
                    pmpoly.styleUrl = "#spray";

                    fldr.Add(pmpoly);
                }
                */
                LineString ls = new LineString();
                ls.AltitudeMode = altmode;
                ls.Extrude = true;
                //ls.Tessellate = true;

                Coordinates coords = new Coordinates();
                coords.AddRange(poslist);

                ls.coordinates = coords;

                Placemark pm = new Placemark();

                string mode = "";
                if (g < modelist.Count)
                    mode = modelist[g];

                pm.name = g + " Flight Path " + mode;
                pm.styleUrl = "#yellowLineGreenPoly";
                pm.LineString = ls;

                stylecode = colours[g%(colours.Length - 1)].ToArgb();

                Style style2 = new Style();
                Color color = Color.FromArgb(0xff, (stylecode >> 16) & 0xff, (stylecode >> 8) & 0xff,
                    (stylecode >> 0) & 0xff);
                log.Info("colour " + color.ToArgb().ToString("X") + " " + color.ToKnownColor().ToString());
                style2.Add(new LineStyle(color, 4));


                pm.AddStyle(style2);

                fldr.Add(pm);
            }

            Placemark pmPOS = new Placemark();
            pmPOS.name = "POS Message";
            pmPOS.LineString = new LineString();
            pmPOS.LineString.coordinates = new Coordinates();
            Point3D lastPoint3D = new Point3D();
            PointLatLngAlt lastplla = PointLatLngAlt.Zero;
            foreach (var item in PosLatLngAlts)
            {
                var newpoint = new Point3D(item.Lng, item.Lat, item.Alt);

                if (item.GetDistance(lastplla) < 0.1 &&
                    lastPoint3D.Z >= (newpoint.Z - 0.3) &&
                    lastPoint3D.Z <= (newpoint.Z + 0.3))
                    continue;

                pmPOS.LineString.coordinates.Add(newpoint);
                lastPoint3D = newpoint;
                lastplla = item;
                if (pmPOS.LineString.coordinates.Count > 20000)
                {
                    //add current
                    pmPOS.AddStyle(style);
                    fldr.Add(pmPOS);

                    // create new
                    pmPOS = new Placemark();
                    pmPOS.name = "POS Message - extra";
                    pmPOS.LineString = new LineString();
                    pmPOS.LineString.coordinates = new Coordinates();
                    lastPoint3D = new Point3D();
                    lastplla = PointLatLngAlt.Zero;
                }
            }
            pmPOS.AddStyle(style);
            fldr.Add(pmPOS);

            Folder planes = new Folder();
            planes.name = "Planes";
            fldr.Add(planes);

            Folder waypoints = new Folder();
            waypoints.name = "Waypoints";
            fldr.Add(waypoints);


            LineString lswp = new LineString();
            lswp.AltitudeMode = AltitudeMode.relativeToGround;
            lswp.Extrude = true;

            Coordinates coordswp = new Coordinates();

            foreach (PointLatLngAlt p1 in cmd)
            {
                if (p1.Lng == 0 && p1.Lat == 0)
                    continue;

                coordswp.Add(new Point3D(p1.Lng, p1.Lat, p1.Alt));
            }

            lswp.coordinates = coordswp;

            Placemark pmwp = new Placemark();

            pmwp.name = "Waypoints";
            //pm.styleUrl = "#yellowLineGreenPoly";
            pmwp.LineString = lswp;

            if (coordswp.Count > 0)
                waypoints.Add(pmwp);

            int a = 0;
            int l = -1;

            Model lastmodel = null;

            foreach (Data mod in flightdata)
            {
                l++;
                if (mod.model.Location.latitude == 0)
                    continue;

                if (lastmodel != null)
                {
                    if (lastmodel.Location.Equals(mod.model.Location))
                    {
                        continue;
                    }
                }
                Placemark pmplane = new Placemark();
                pmplane.name = "Plane " + a;

                pmplane.visibility = false;

                Model model = mod.model;
                model.AltitudeMode = altmode;
                model.Scale.x = 2;
                model.Scale.y = 2;
                model.Scale.z = 2;

                try
                {
                    pmplane.description = @"<![CDATA[
              <table>
                <tr><td>Roll: " + model.Orientation.roll + @" </td></tr>
                <tr><td>Pitch: " + model.Orientation.tilt + @" </td></tr>
                <tr><td>Yaw: " + model.Orientation.heading + @" </td></tr>
                <tr><td>WP dist " + mod.ntun[2] + @" </td></tr>
				<tr><td>tar bear " + mod.ntun[3] + @" </td></tr>
				<tr><td>nav bear " + mod.ntun[4] + @" </td></tr>
				<tr><td>alt error " + mod.ntun[5] + @" </td></tr>
              </table>
            ]]>";
                }
                catch
                {
                }

                try
                {
                    pmplane.Point = new KmlPoint((float) model.Location.longitude, (float) model.Location.latitude,
                        (float) model.Location.altitude);
                    pmplane.Point.AltitudeMode = altmode;

                    Link link = new Link();
                    link.href = "block_plane_0.dae";

                    model.Link = link;

                    pmplane.Model = model;

                    planes.Add(pmplane);
                }
                catch
                {
                } // bad lat long value

                lastmodel = mod.model;

                a++;
            }

            kml.Document.Add(fldr);

            kml.Save(filename);

            // create kmz - aka zip file

            FileStream fs = File.Open(filename.ToLower().Replace(".log.kml", ".kmz").Replace(".bin.kml", ".kmz"),
                FileMode.Create);
            ZipOutputStream zipStream = new ZipOutputStream(fs);
            zipStream.SetLevel(9); //0-9, 9 being the highest level of compression
            zipStream.UseZip64 = UseZip64.Off; // older zipfile

            // entry 1
            string entryName = ZipEntry.CleanName(Path.GetFileName(filename));
                // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();

            File.Delete(filename);

            filename = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar +
                       "block_plane_0.dae";

            // entry 2
            entryName = ZipEntry.CleanName(Path.GetFileName(filename));
                // Removes drive from name and fixes slash direction
            newEntry = new ZipEntry(entryName);
            newEntry.DateTime = DateTime.Now;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();


            zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
            zipStream.Close();

            positionindex = 0;
            modelist.Clear();
            flightdata.Clear();
            position = new List<Core.Geometry.Point3D>[200];
            cmd.Clear();
        }