简体   繁体   English

C# MySql Dapper MySqlGeometry

[英]C# MySql Dapper MySqlGeometry

I am trying to get the Polygon as MySqlGeometry using Dapper.我正在尝试使用 Dapper 将多边形作为 MySqlGeometry。
But the MySql connector understands only MySqlGeometry as Point.但是 MySql 连接器仅将 MySqlGeometry 理解为 Point。
And I need the other types also, like Polygon.我还需要其他类型,比如多边形。
This is what I got from other forums:这是我从其他论坛得到的:

// 1. Add Dapper custom Type handler.
Dapper.SqlMapper.AddTypeHandler(new MySqlGeometryTypeHandler());    

// 2. Implement that custome handler.
public class MySqlGeometryTypeHandler : SqlMapper.TypeHandler<MySqlGeometry>
{
    public override MySqlGeometry Parse(object value) { return new MySqlGeometry(MySqlDbType.Geometry, (byte[])value); }
    public override void SetValue(System.Data.IDbDataParameter parameter, MySqlGeometry value) { parameter.Value = value; }
}

// 3. Here is the Data class
public class Geo
{ 
    public int Id { get; set; } 
    public MySqlGeometry G { get; set; }
}

// 4. Here is the Dapper query
List<Geo> datas = Dapper.SqlMapper.Query<Geo>(cnn, "select * from geo;").ToList();

How do I get the Polygon rows I have in the 'geo' table?如何获取“地理”表中的多边形行?

It comes out - MySqlGeometry doesn't support (hopefully yet) other types than Point.结果出来了——MySqlGeometry 不支持(希望还)除 Point 之外的其他类型。
So the solution I got is:所以我得到的解决方案是:

  1. Use instead of MySqlGeometry the Microsoft's System.Data.Spatial.DbGeography in the model.在 model 中使用 Microsoft 的 System.Data.Spatial.DbGeography 代替 MySqlGeometry。
    public class Geo
    {
        public int Id { get; set; }
        public DbGeography G { get; set; }
    }
  1. Change the custom Type handler and registration accordingly:相应地更改自定义类型处理程序和注册:
    public class DbGeographyTypeHandler : SqlMapper.TypeHandler<DbGeography>
    {
        public override DbGeography Parse(object value) { return DbGeography.FromBinary((byte[])value); }
        public override void SetValue(IDbDataParameter parameter, DbGeography value) { parameter.Value = value.AsBinary(); }
    }

Dapper.SqlMapper.AddTypeHandler<DbGeography>(new DbGeographyTypeHandler());

  1. And change the sql to return the value as standard WKB, which Microsoft's DbGeography understands.并更改 sql 以将值作为标准 WKB 返回,微软的 DbGeography 可以理解。 You can also use st_asbinary instead of st_aswkb - both worked for me.您也可以使用 st_asbinary 而不是 st_aswkb - 两者都对我有用。
List<Geo> datas = Dapper.SqlMapper.Query<Geo>(conn, "select id, st_aswkb(g) g from geo;").ToList();
  1. Just to note:只是要注意:
  • The MySql is 8, and the Dapper is 2.0.78. MySql 为 8,Dapper 为 2.0.78。
  • I used DbGeography - because I need earth geo locations, as opposed to DbGeometry.我使用了 DbGeography - 因为我需要地球地理位置,而不是 DbGeometry。
  • I used the MySql column datatype as Geometry, because I need spatial index on it for faster data retrieval.我使用 MySql 列数据类型作为几何,因为我需要空间索引来加快数据检索。
  • If you need spatial index, make sure you set your Geometry column as non null, and with srid (eg 4326), because default srid 0 will cause your query to ignore the spatial index.如果您需要空间索引,请确保将几何列设置为非 null,并使用 srid(例如 4326),因为默认 srid 0 将导致您的查询忽略空间索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM