0

I create polygons with mouse click events as follows:

    private void PrintMousePos(int x, int y, MouseEventArgs e)
    {
        PointF[] location = { new PointF(x, y) };
        inverseTransform.TransformPoints(location);

        if (NewPolygon != null)
        {
            // We are already drawing a polygon.
            // If it's the right mouse button, finish this polygon.
            if (e.Button == MouseButtons.Right)
            {
                // Finish this polygon.
                if (NewPolygon.Count > 2) Polygons.Add(NewPolygon);
                NewPolygon = null;
            }
            else
            {
                // Add a point to this polygon.
                if (NewPolygon[NewPolygon.Count - 1] != Location)
                {
                    NewPolygon.Add(location[0]);
                }
            }
        }
        else
        {
            // Start a new polygon.
            NewPolygon = new List<PointF>();
            NewPoint = Location;
            NewPolygon.Add(location[0]);
        }


        // Redraw.
        picCanvas.Invalidate();

    }

(picCanvas is a picturebox within a Panel. I don't use any maps, just an image)

I need to save this polygons to MySQL database. I'm using MySQL workbench 8 to create the table.

How do I save the every polygon to a table and what datatype is the best to use for this?

How do I retrieve the polygon points again to and populate a table?

Please give me an example of the sql text to save and to retrieve the data?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You could just store it by serialising. – Charleh Nov 10 '18 at 11:01
  • What manipulation of the data do you expect to do in MySQL? – Caius Jard Nov 10 '18 at 11:01
  • MySql features a couple of geometry types e.g. [Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html) and a few others meant for this kind of data. But this may well be overkill as the Spatial Data Types are meant for professional applications. So maybe a string/varchar with the points separated by delimiter1 and its coordinates separated by delimiter2 are simpler to use.. – TaW Nov 10 '18 at 11:14
  • can save this format Youpolygon((19.31309113115049 14.015986184198945, 19.31196348511111 14.0158918019151, 19.31115411341461 14.0151416133191, 19.31161380114411 14.0156419915105, 19.31151181460311 14.0155386981454, 19.31131511991113 14.0154141993118, 19.31116418143134 14.015310109439, 19.31104080581665 14.015116019458, 19.31181981311516 14.0151061149111, 19.31113503398895 14.0150134518686)) – LDS Nov 10 '18 at 12:57
  • Sorry I'm c# beginner. I create with the code, a couple of polygons. I need to save it in the MySQL database with Win form app. I'm get floating point data from serial port within a console app and then check if this point in in one of the polygons. I use my Win Forms app to create and save the polygons and console app to check if the receiving point is within the polygons – Izak Viljoen Nov 10 '18 at 13:42
  • Do switch from arrays to List! It is much more powerful.. - This: `public static string Serialize(T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using(StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } }` will turns a List into a string. See [here](https://stackoverflow.com/questions/2434534/serialize-an-object-to-string) for the source of it and also the reverse transformation. Can you read/write to the DB? – TaW Nov 10 '18 at 15:24
  • You would call the conversion like this: `string ps = Serialize>( yourPointsList);` – TaW Nov 10 '18 at 15:25
  • Thank you- I works great – Izak Viljoen Nov 12 '18 at 09:25

0 Answers0