Skip to content

transform() function rotates shapes incorrectly -> incorrect extend #353

Closed

Description

@timtreis

MRE

import geopandas as gpd
import spatialdata
import spatialdata_plot
from shapely import MultiPolygon, Point, Polygon

points = []
for p in [[0, 0], [0, 1], [1, 1], [1, 0]]:
    points.append(Point(p))

gdf = gpd.GeoDataFrame(geometry=points)
gdf["radius"] = 0.05
circles_df = ShapesModel.parse(gdf)
sdata = spatialdata.SpatialData(shapes={"circles": circles_df})

theta = math.pi / 4
rotation = Affine(
[
    [math.cos(theta), -math.sin(theta), 0],
    [math.sin(theta), math.cos(theta), 0],
    [0, 0, 1],
],
input_axes=("x", "y"),
output_axes=("x", "y"),
)
set_transformation(element=sdata["circles"], transformation=rotation, to_coordinate_system="transformed")

sdata.pl.render_shapes().pl.show()

for cs in ["global", "transformed"]:
    print(cs, get_extent(sdata, coordinate_system=cs))

Produces the following plot:
image

We expect the axis of the transformed plot to also touch the circles. This is not the case since the extent is calculated to transformed {'y': (-0.07071067811865475, 1.48492424049175), 'x': (-0.7778174593052023, 0.7778174593052024)}.

However, the error doesn't come from the extent calculation, but from L305 in _compute_extent_in_coordinate_system:
image

Bounding box before and after transformation:
image

Since we're rotating by 45°, we'd expect them to be

[ 0,      0]
[−0.7071, 0.7071]
[ 0,      1.4142]
[ 0.7071, 0.7071]

(+/- the radius of the circle 0.05) which would result in a different bounding box. With larger radii, the inaccuracy becomes bigger

radius = 0.5

image

Activity

  1. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    Quick question @timtreis. Running the code produces this:
    image

    I think I need to use one of the new branches of spatialdata-plot that will make it into the next release. Which one?

  2. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    I guess scverse/spatialdata-plot#162, gonna try now.

  3. timtreis commented on Oct 9, 2023

    @timtreis
    MemberAuthor

    Yes, this one. Can you reproduce?

  4. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    Yes thanks. This is the cause of the bug:
    image

  5. timtreis commented on Oct 9, 2023

    @timtreis
    MemberAuthor

    Ahhh yeah, makes sense. That's why it scales with r

  6. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    The current approach works for raster data, the affected part is points, shapes and polygons. I will implement the exact calculation for the vector data, enabled by default, but I will also make it optional with a parameter of get_extent.

    I suspect that the current implementation can be a lot faster since it doesn't need to transform the data, and for most cases actually one doesn't need the exact extent.

  7. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    Fixed
    image
    Gonna make a PR to the get_extent() PR, I will also fix the failing tests.

  8. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    I am adding tests, not just for circles but also for points, polygons and multipolygons.

    These are the plots that one would get with the old code (note that I have changed the positions of the circles in the "global" coordinate system).
    4 circles
    image
    4 polygons
    image
    4 multipolygons
    image
    4 points
    image

    This is with the new code.
    4 circles
    image
    4 polygons
    image
    4 multipolygons
    image
    4 points
    image

    Things work, except for points.
    I will:

    • check if get_extent() works for points, and if it does report the bug in spatialdata-plot
    • open an issue in spatialdata-plot to add tests for the cases above; in fact in the tests in spatialdata I will just check that the coordinates are correct; I am making the plots now just to see what's going on
  9. linked a pull request that will close this issueFixes bug with extent of rotated data #373on Oct 9, 2023
  10. LucaMarconato commented on Oct 9, 2023

    @LucaMarconato
    Member

    All correctly computed. I have opened the aforementioned issues in spatialdata-plot and I have made a PR to merge against the get_extent() PR.

  11. LucaMarconato commented on Oct 10, 2023

    @LucaMarconato
    Member

    Fixed by #373 and merged via #318

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

Metadata

Metadata

Assignees

Labels

bug 🚨Something isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions