Skip to content

Spatial indexing for WKT-based geometries works correctly for POLYGON shapes, but fails for MULTIPOLYGON geometries #21613

Description

@ivanlegin

MULTIPOLYGON geometries are indexed with incorrect stored spatial data, which is visible in RavenDB Studio when “Show stored index fields only” is enabled. The same scenario only with POLYGON geometries works correctly.

RavenTestDriver version: 7.1.3

public class DocumentWithSpatial
  {
      public string Id { get; set; } = null!;

      public string SpatialWkt { get; set; } = null!;
  }

  public class Areas_ByGeometry : AbstractIndexCreationTask<DocumentWithSpatial>
  {
      public class Result
      {
          public string Id { get; set; } = null!;
          public object Geometry { get; set; } = null!;
      }

      public Areas_ByGeometry()
      {
          // Corax doesn't support WKT-based spatial indexing yet.
          SearchEngineType = Raven.Client.Documents.Indexes.SearchEngineType.Lucene;
          Map = documentsWithSpatial =>
              from documentWithSpatial in documentsWithSpatial
              select new
              {
                  documentWithSpatial.Id,
                  Geometry = CreateSpatialField(documentWithSpatial.SpatialWkt),
              };
      }
  }

  [Fact]
  public async Task SortByDistance_FailsForMultiPolygonIndexing()
  {
      var furthest_field_id = "furthest-field-id";
      var furthest_field_wkt =
          "POLYGON ((17.384491 45.576682, 17.391958 45.577643, 17.387323 45.580166, 17.384491 45.576682))";

      var closer_field_id = "closer-field-id";
      var closer_field_wkt =
          "MULTIPOLYGON (((17.384491 45.576682, 17.391958 45.577643, 17.387323 45.580166, 17.384491 45.576682)),((17.401743 45.580947, 17.408781 45.579565, 17.414446 45.584731, 17.409124 45.587014, 17.401571 45.585693, 17.401743 45.580947)))";

      var closest_field_id = "closest-field-id";
      var closest_field_wkt =
          "POLYGON ((17.420797 45.582028, 17.422857 45.579385, 17.428007 45.579325, 17.429895 45.582509, 17.428522 45.584731, 17.423286 45.584551, 17.420797 45.582028))";

      var longitude = 17.418308;
      var latitude = 45.582269;

      var documentsWithSpatial = new List<DocumentWithSpatial>
      {
          new() { Id = closer_field_id, SpatialWkt = closer_field_wkt },
          new() { Id = furthest_field_id, SpatialWkt = furthest_field_wkt },
          new() { Id = closest_field_id, SpatialWkt = closest_field_wkt },
      };

      using var store = StoreItemsToTestDb(documentsWithSpatial);
      await store.ExecuteIndexAsync(new Areas_ByGeometry());
      WaitForIndexing(store);

      using var session = store.OpenAsyncSession();


      //This will throw Spatial4n.Exceptions.ParseException for MULTIPOLYGON
      var result = await session
          .Query<Areas_ByGeometry.Result, Areas_ByGeometry>()
          .OrderByDistance(x => x.Geometry, latitude, longitude)
          .ToArrayAsync();

      Assert.NotNull(result);
      Assert.Equal(3, result.Length);
      Assert.Equal(closest_field_id, result[0].Id);
      Assert.Equal(closer_field_id, result[1].Id);
      Assert.Equal(furthest_field_id, result[2].Id);
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions