NPGSQL: a related operation called the LWGEOMCOLLECTION type

I have a query that retrieves PostGIS data using Npgsql. Its purpose is to take a point (x, y coordinates) and determine what (if any) geometry is at this point. For the vast majority of geometries in the database, the query works fine, but for at least one I get the following exception:

ERROR: XX000: A communication operation called the LWGEOMCOLLECTION type. This is not supported.

with stack trace top:

[NpgsqlException (0x80004005): ERROR: XX000: The associated operation is called of type LWGEOMCOLLECTION. This is not supported.]
Npgsql.d__0.MoveNext () +3160
Npgsql.ForwardsOnlyDataReader.GetNextResponseObject (Boolean cleanup) +808 Npgsql.ForwardsOnlyDataReader.GetNextRow (Boolean clearPending) +308 Npgsql.ForwardsOnlyDataReader.Read () +47

All geometries must be valid, since I call ST_MakeValid for all that are not, and currently not, where ST_IsValid returns false. The geometry was created by calling ST_GeomFromKML and displayed on the map as a raster layer using WMS via GeoServer or as a vector layer using ST_AsGeoJSON , so PostGIS data looks fine.

Is there a way to change my code or data to stop this? Part of the code malfunction is part of the reader reading:

 command.CommandText = "SELECT area_code FROM area WHERE ST_INTERSECTS(ST_SetSRID(ST_Point(:x, :y), 4326), shape) AND area_type_code = :typecode"; command.CommandType = CommandType.Text; var typeCodeParameter = new NpgsqlParameter { DbType = DbType.String, ParameterName = "typecode", Value = _typeCode }; var xParameter = new NpgsqlParameter { DbType = DbType.Double, ParameterName = "x", Value = _x }; var yParameter = new NpgsqlParameter { DbType = DbType.Double, ParameterName = "y", Value = _y }; command.Parameters.Add(typeCodeParameter); command.Parameters.Add(xParameter); command.Parameters.Add(yParameter); using (var reader = command.ExecuteReader()) { if (reader.Read()) area = new AreaBasic { Code = (string)reader["area_code"] }; } 

EDIT: additional information. The same error occurs when starting a query with hard-coded values โ€‹โ€‹in pgAdmin III, so the problem is not specific to Npgsql.

+6
source share
1 answer

This is due to an attempt to cause intersections or contains a type request in a geometry collection, i.e. where you have a mixture of points, lines, and polygons (maybe a few).

There are at least a few possible fixes. The first one is simpler, but it seems a bit hacked, which simply reduces the input geometry first to 0, which will remove the non-polygons, so in your case, just change the command.commandText command to

 SELECT area_code FROM area WHERE ST_INTERSECTS(ST_SetSRID(ST_Point(:x, :y), 4326), ST_Buffer(shape, 0)) AND area_type_code = :typecode"; 

Please note: this approach can often be used to correct incorrect geometries using self-intersecting loops, etc.

The second approach is to use ST_Dump in the form field to split into separate geometries, and then use only polygons in the real query using ST_GeometryType .

 SELECT area_code FROM (SELECT area_code, (ST_Dump(area)).geom FROM area) poly WHERE ST_INTERSECTS(ST_SetSRID(ST_Point(:x, :y), 4326), poly.geom) AND ST_GeometryType(poly.geom) = 'ST_Polygon' OR ST_GeometryType(poly.geom) = 'ST_MultiPolygon' AND area_type_code = :typecode"; 

This is not verified as I cannot verify this clearly on your data, but these approaches work in practice.

+6
source

Source: https://habr.com/ru/post/982282/


All Articles