昨天在项目中,用到 EntityFramework 通过SQL语句查询。
具体的SQL语句如下:
SELECT t.* FROM ( SELECT c.id AS CommunityId, c.`name` AS CommunityName, ASTEXT(c.spatial_data) AS CommunitySpatialData, s.id AS StreetId, s.`name` AS StreetName, ASTEXT(s.spatial_data) AS StreetSpatialData, MBRContains (s.spatial_data ,ST_GeometryFromText('Point(@Longitude @Latitude)')) AS streetIn, MBRContains (c.spatial_data ,ST_GeometryFromText('Point(@Longitude @Latitude)')) AS communityIn FROM community c LEFT JOIN street s ON c.street_id = s.id ) AS t WHERE t.streetIn = 1 AND t.communityIn = 1;
应该说这样没有问题的,但是查询一直报错:“Invalid GIS data provided to function st_geometryfromtext.”
换了多种传参方式,但是都还有问题。
经过各种调试、修改参数,大致可以确定是:两个参数是连续的,中间有空格,但是在参数替换后空格替换没了,导致数据格式出错。
再次调整下传的参数就可以,修改后SQL语句:
SELECT t.* FROM ( SELECT c.id AS CommunityId, c.`name` AS CommunityName, ASTEXT(c.spatial_data) AS CommunitySpatialData, s.id AS StreetId, s.`name` AS StreetName, ASTEXT(s.spatial_data) AS StreetSpatialData, MBRContains (s.spatial_data ,ST_GeometryFromText(@point)) AS streetIn, MBRContains (c.spatial_data ,ST_GeometryFromText(@point)) AS communityIn FROM community c LEFT JOIN street s ON c.street_id = s.id ) AS t WHERE t.streetIn = 1 AND t.communityIn = 1;
对应的 point 是:
string point = "Point(" + Longitude + " " + Latitude + ")";
虽然知道了问题,也解决了,但是没有彻底解决这个问题。
有知道的可以告知下。
注意:
1、对于面数据:最后一个点应该是第一个点,这样在入库的时候才正确,否则报错“Invalid GIS data provided to function st_geometryfromtext.”