Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with remove_layer #35

Open
asm201 opened this issue Jun 26, 2020 · 1 comment
Open

Problem with remove_layer #35

asm201 opened this issue Jun 26, 2020 · 1 comment

Comments

@asm201
Copy link

asm201 commented Jun 26, 2020

Describe the bug
I was trying to remove a layer that already exist so that i can create another, however when i use remove_layer(), the existing layer is removed but the drawing is not.

To Reproduce

class DesenhaLinha(Screen):

    def __init__(self, **kwargs):
        super(DesenhaLinha, self).__init__(**kwargs)

    def desenha(self, valor):
        line = LineMapLayer()
        if not (line.coordinates == []):
            self.ids.mapview.remove_layer(line)
            line.coordinates.clear()
        print(line.coordinates)
        layer = GeoJsonMapLayer(geojson=linha.aggregate([{'$limit': int(valor)}]))
        for l in layer.geojson:
            x, y = l['geometry']['coordinates']
            line.coordinates.append(x)
            line.coordinates.append(y)
        self.ids.mapview.add_layer(line, mode="scatter")



class LineMapLayer(MapLayer):
    def __init__(self, **kwargs):
        super(LineMapLayer, self).__init__(**kwargs)
        self.zoom = 0

        # NOTE: Points must be valid as they're no longer clamped
        self.coordinates = []

    def reposition(self):
        mapview = self.parent

        #: Must redraw when the zoom changes
        #: as the scatter transform resets for the new tiles
        if (self.zoom != mapview.zoom):
            self.draw_line()

    def get_x(self, lon):
        """Get the x position on the map using this map source's projection
        (0, 0) is located at the top left.
        """
        return clamp(lon, MIN_LONGITUDE, MAX_LONGITUDE)

    def get_y(self, lat):
        """Get the y position on the map using this map source's projection
        (0, 0) is located at the top left.
        """
        lat = clamp(-lat, MIN_LATITUDE, MAX_LATITUDE)
        lat = lat * pi / 180.
        return ((1.0 - log(tan(lat) + 1.0 / cos(lat)) / pi))

    def draw_line(self, *args):
        mapview = self.parent
        self.zoom = mapview.zoom

        # When zooming we must undo the current scatter transform
        # or the animation distorts it
        scatter = mapview._scatter
        map_source = mapview.map_source
        sx, sy, ss = scatter.x, scatter.y, scatter.scale
        vx, vy, vs = mapview.viewport_pos[0], mapview.viewport_pos[1], mapview.scale

        # Account for map source tile size and mapview zoom
        ms = pow(2.0, mapview.zoom) * map_source.dp_tile_size

        #: Since lat is not a linear transform we must compute manually
        line_points = []
        for lon, lat in self.coordinates:
            line_points.extend((self.get_x(lon), self.get_y(lat)))
            # line_points.extend(mapview.get_window_xy_from(lat,lon,mapview.zoom))
        with self.canvas:
            # Clear old line
            self.canvas.clear()
            # Undo the scatter animation transform
            Scale(1 / ss, 1 / ss, 1)
            Translate(-sx, -sy)

            # Apply the get window xy from transforms
            Scale(vs, vs, 1)
            Translate(-vx, -vy)

            # Apply the what we can factor out
            # of the mapsource long,lat to x,y conversion
            Scale(ms / 360.0, ms / 2.0, 1)
            Translate(180, 0)

            # Draw new
            Color(0, 0, 0, .6)
            Line(points=line_points, width=1)  # 4/ms)#, joint="round",joint_precision=100)

Expected behavior
i was expcting to remove both the layer and the drawing

Platform:

  • OS: windows 10
  • Python 3.7.7
  • 1.0.5

if you want or need to see screenshots please tell me and i will put on the next comment

@asm201 asm201 changed the title Problem with remove_layout Problem with remove_layer Jul 7, 2020
@Pater-Forg
Copy link

Hello! Seems I have the same problem. Did you solve it? Tell me how, please. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants