如何读取 MySQL table 多边形数据

How to read from MySQL table Polygon data

我正在开发一个应用程序,我需要将位置数据存储在 MySQL table 上。除了点位置,我还需要区域(多边形)。

我目前写的多边形坐标如下:

 oMySQLConnecion = new MySqlConnection(DatabaseConnectionString);
            if (oMySQLConnecion.State == System.Data.ConnectionState.Closed || oMySQLConnecion.State == System.Data.ConnectionState.Broken)
            {
                oMySQLConnecion.Open();

            }
            if (oMySQLConnecion.State == System.Data.ConnectionState.Open)
            {                    
                string Query = @"INSERT INTO region (REGION_POLYGON) VALUES (PolygonFromText(@Parameter1))";

                MySqlCommand oCommand = new MySqlCommand(Query, oMySQLConnecion);
                oCommand.Parameters.AddWithValue("@Parameter1", PolygonString);

                int sqlSuccess = oCommand.ExecuteNonQuery();
                oMySQLConnecion.Close();

                oDBStatus.Type = DBDataStatusType.SUCCESS;
                oDBStatus.Message = DBMessageType.SUCCESSFULLY_DATA_UPDATED;
                return oDBStatus;
            }

执行后,我在MySQL table中看到了Blob。

现在我想读回数据以进行测试,但它无法按照我在下面尝试的方式工作:

 if (oMySQLConnecion.State == System.Data.ConnectionState.Open)
            {
                string Query = @"SELECT REGION_ID,REGION_NICK_NAME,GeomFromText(REGION_POLYGON) AS POLYGON FROM region WHERE REGION_USER_ID = @Parameter1";

                MySqlCommand oCommand = new MySqlCommand(Query, oMySQLConnecion);
                oCommand.Parameters.AddWithValue("@Parameter1", UserID);

                using (var reader = oCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        R_PolygonCordinates oRec = new R_PolygonCordinates();
                        oRec.RegionNumber = Convert.ToInt32(reader["REGION_ID"]);
                        oRec.RegionNickName = reader["REGION_NICK_NAME"].ToString();
                        oRec.PolygonCodinates = reader["POLYGON"].ToString();
                        polygons.Add(oRec);
                    }
                }
                int sqlSuccess = oCommand.ExecuteNonQuery();
                oMySQLConnecion.Close();
                return polygons;
            }

它returns一个空字符串。

  1. 我不确定我是否真的在写数据,因为我无法读取 Blob。
  2. 我的阅读语法不正确吗?

** 注意:** 我使用的是 Visual Studio 2017。MySQL 最新版本带有 Spacial 类。

非常感谢任何帮助。

谢谢

GeomFromText() 采用 WKT(标准化 "well-known text" 格式)值作为输入,returns MySQL 内部几何类型作为输出。

这与您需要的相反,即 ST_AsWKT()ST_AsText() -- 将内部格式几何对象作为输入,return WKT 作为输出。

在 5.6 之前,该函数称为 AsWKT()AsText()。在 5.7 中,这些都是完全相同函数的同义词,但非 ST_* 函数已弃用,将来会被删除。

https://dev.mysql.com/doc/refman/5.7/en/gis-format-conversion-functions.html#function_st-astext

我不确定 ST_ 前缀是什么意思,但我认为它是 "spatial type." WL#8055 中的一些讨论可能很有趣。