Example #1
0
 def test_split_tee_1(self):
     """
              C
     A +---===+===---+ B
          A'  |  B'
              +      AB exists with topology A'B'.
              D      Add CD.
     """
     ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
     # Create a topology
     topology = TopologyFactory.create(no_path=True)
     topology.add_path(ab, start=0.25, end=0.75)
     topogeom = topology.geom
     # Topology covers 1 path
     self.assertEqual(len(topology.paths.all()), 1)
     cd = PathFactory.create(geom=LineString((2, 0, 0), (2, 2, 0)))
     cb = Path.objects.filter(name="AB").exclude(pk=ab.pk)[0]
     # Topology now covers 2 paths
     self.assertEqual(len(topology.paths.all()), 2)
     # AB and AB2 has one topology each
     self.assertEqual(len(ab.aggregations.all()), 1)
     self.assertEqual(len(cb.aggregations.all()), 1)
     # Topology position became proportional
     aggr_ab = ab.aggregations.all()[0]
     aggr_cb = cb.aggregations.all()[0]
     self.assertEqual((0.5, 1.0), (aggr_ab.start_position, aggr_ab.end_position))
     self.assertEqual((0.0, 0.5), (aggr_cb.start_position, aggr_cb.end_position))
     topology.reload()
     self.assertNotEqual(topology.geom, topogeom)
     self.assertEqual(topology.geom.coords[0], topogeom.coords[0])
     self.assertEqual(topology.geom.coords[-1], topogeom.coords[-1])
Example #2
0
 def test_manager_can_delete(self):
     self.login()
     path = PathFactory()
     response = self.client.get(path.get_detail_url())
     self.assertEqual(response.status_code, 200)
     response = self.client.post(path.get_delete_url())
     self.assertEqual(response.status_code, 302)
Example #3
0
 def test_snapping_is_idempotent(self):
     PathFactory.create(geom=LineString((0, 0), (9.8, 0), (9.9, 0), (10, 0)))
     path_snapped = PathFactory.create(geom=LineString((10, 0.1), (10, 10)))
     old_geom = path_snapped.geom
     path_snapped.geom = old_geom
     path_snapped.save()
     self.assertEqual(path_snapped.geom.coords, old_geom.coords)
Example #4
0
    def test_split_tee_2(self):
        """
              C
        A +---+---=====--+ B
              |   A'  B'
              +           AB exists with topology A'B'.
              D           Add CD
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        # Create a topology
        topology = TopologyFactory.create(no_path=True)
        topology.add_path(ab, start=0.5, end=0.75)
        topogeom = topology.geom
        # Topology covers 1 path
        self.assertEqual(len(ab.aggregations.all()), 1)
        self.assertEqual(len(topology.paths.all()), 1)
        self.assertEqual(topology.paths.all()[0], ab)
        cd = PathFactory.create(geom=LineString((1, 0, 0), (1, 2, 0)))
        # CB was just created
        cb = Path.objects.filter(name="AB").exclude(pk=ab.pk)[0]

        # AB has no topology anymore
        self.assertEqual(len(ab.aggregations.all()), 0)
        # Topology now still covers 1 path, but the new one
        self.assertEqual(len(topology.paths.all()), 1)
        self.assertEqual(len(cb.aggregations.all()), 1)
        self.assertEqual(topology.paths.all()[0].pk, cb.pk)
        topology.reload()
        self.assertEqual(topology.geom, topogeom)
Example #5
0
    def test_split_on_update_7(self):
        """
                                          C
        A +-----------+ B         A +-----X---+ B
                                          :
        C X-------+ D                     :
                                          + D
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        cd = PathFactory.create(name="CD", geom=LineString((0, 1, 0), (4, 1, 0)))
        topology = TopologyFactory.create(no_path=True)
        topology.add_path(cd, start=0.0, end=0.0)
        self.assertEqual(len(topology.paths.all()), 1)

        cd.geom = LineString((2, 0, 0), (2, -2, 0))
        cd.save()
        cb = Path.objects.filter(name="AB").exclude(pk=ab.pk)[0]

        self.assertEqual(len(ab.aggregations.all()), 1)
        self.assertEqual(len(cb.aggregations.all()), 1)
        self.assertEqual(len(cd.aggregations.all()), 1)
        self.assertEqual(len(topology.paths.all()), 3)

        aggr_ab = ab.aggregations.all()[0]
        aggr_cb = cb.aggregations.all()[0]
        aggr_cd = cd.aggregations.all()[0]
        self.assertEqual((1.0, 1.0), (aggr_ab.start_position, aggr_ab.end_position))
        self.assertEqual((0.0, 0.0), (aggr_cb.start_position, aggr_cb.end_position))
        self.assertEqual((0.0, 0.0), (aggr_cd.start_position, aggr_cd.end_position))
Example #6
0
    def test_helpers(self):
        trek = TrekFactory.create(no_path=True)
        p1 = PathFactory.create(geom=LineString((0, 0), (4, 4)))
        p2 = PathFactory.create(geom=LineString((4, 4), (8, 8)))
        poi = POIFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=trek, path=p1,
                                      start_position=0.5)
        PathAggregationFactory.create(topo_object=trek, path=p2)
        PathAggregationFactory.create(topo_object=poi, path=p1,
                                      start_position=0.6, end_position=0.6)
        # /!\ District are automatically linked to paths at DB level
        d1 = DistrictFactory.create(geom=MultiPolygon(
            Polygon(((-2, -2), (3, -2), (3, 3), (-2, 3), (-2, -2)))))

        # Ensure related objects are accessible
        self.assertItemsEqual(trek.pois, [poi])
        self.assertItemsEqual(poi.treks, [trek])
        self.assertItemsEqual(trek.districts, [d1])

        # Ensure there is no duplicates
        PathAggregationFactory.create(topo_object=trek, path=p1,
                                      end_position=0.5)
        self.assertItemsEqual(trek.pois, [poi])
        self.assertItemsEqual(poi.treks, [trek])

        d2 = DistrictFactory.create(geom=MultiPolygon(
            Polygon(((3, 3), (9, 3), (9, 9), (3, 9), (3, 3)))))
        self.assertItemsEqual(trek.districts, [d1, d2])
Example #7
0
 def test_default_stake(self):
     i = InterventionFactory.create()
     i.stake = None
     self.assertTrue(i.stake is None)
     i.save()
     self.assertTrue(i.stake is None)
     
     lowstake = StakeFactory.create()
     highstake = StakeFactory.create()
     if lowstake > highstake:
         tmp = lowstake
         lowstake = highstake
         highstake = tmp
     
     # Add paths to topology
     infra = InfrastructureFactory.create(no_path=True)
     infra.add_path(PathFactory.create(stake=lowstake))
     infra.add_path(PathFactory.create(stake=highstake))
     infra.add_path(PathFactory.create(stake=lowstake))
     i.set_infrastructure(infra)
     # Stake is not None anymore
     i.save()
     self.assertFalse(i.stake is None)
     # Make sure it took higher stake
     self.assertEqual(i.stake, highstake)
Example #8
0
    def test_delete_cascade(self):
        p1 = PathFactory.create()
        p2 = PathFactory.create()
        t = TrekFactory.create(no_path=True)
        t.add_path(p1)
        t.add_path(p2)

        # Everything should be all right before delete
        self.assertTrue(t.published)
        self.assertFalse(t.deleted)
        self.assertEqual(t.aggregations.count(), 2)

        # When a path is deleted
        p1.delete()
        t = Trek.objects.get(pk=t.pk)
        self.assertFalse(t.published)
        self.assertFalse(t.deleted)
        self.assertEqual(t.aggregations.count(), 1)

        # Reset published status
        t.published = True
        t.save()

        # When all paths are deleted
        p2.delete()
        t = Trek.objects.get(pk=t.pk)
        self.assertFalse(t.published)
        self.assertTrue(t.deleted)
        self.assertEqual(t.aggregations.count(), 0)
Example #9
0
    def test_point_geom_3d(self):
        """
           +
          / \
         / X \
        +     +
        """
        p1 = PathFactory.create(geom=LineString((0, 0, 1000), (4, 4, 2000)))
        p2 = PathFactory.create(geom=LineString((4, 4, 2000), (8, 0, 0)))

        poi = Point(3, 1, srid=settings.SRID)
        position, distance = Path.interpolate(p1, poi)
        self.assertTrue(almostequal(0.5, position))
        self.assertTrue(almostequal(-1.414, distance))
        # Verify that deserializing this, we obtain the same original coordinates
        # (use lat/lng as in forms)
        poi.transform(settings.API_SRID)
        poitopo = Topology.deserialize({'lat': poi.y, 'lng': poi.x})
        # Computed topology properties match original interpolation
        self.assertTrue(almostequal(0.5, poitopo.aggregations.all()[0].start_position))
        self.assertTrue(almostequal(-1.414, poitopo.offset))
        # Resulting geometry
        self.assertTrue(almostequal(3, poitopo.geom.x))
        self.assertTrue(almostequal(1, poitopo.geom.y))
        self.assertTrue(almostequal(0, poitopo.geom.z))
Example #10
0
    def test_junction_point(self):
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (2, 2, 2)))
        p2 = PathFactory.create(geom=LineString((0, 0, 0), (2, 0, 0)))
        p3 = PathFactory.create(geom=LineString((0, 2, 2), (0, 0, 0)))

        # Create a junction point topology
        t = TopologyFactory.create(no_path=True)
        self.assertEqual(len(t.paths.all()), 0)

        pa = PathAggregationFactory.create(topo_object=t, path=p1,
                                      start_position=0.0, end_position=0.0)

        self.assertItemsEqual(t.paths.all(), [p1, p2, p3])

        # Update to a non junction point topology
        pa.end_position = 0.4
        pa.save()

        self.assertItemsEqual(t.paths.all(), [p1])

        # Update to a junction point topology
        pa.end_position = 0.0
        pa.save()

        self.assertItemsEqual(t.paths.all(), [p1, p2, p3])
Example #11
0
 def test_latestupdate_delete(self):
     for i in range(10):
         PathFactory.create()
     t1 = dbnow()
     self.assertTrue(t1 > Path.latest_updated())
     (Path.objects.all()[0]).delete()
     self.assertFalse(t1 > Path.latest_updated())
Example #12
0
    def test_topology_geom(self):
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (2, 2, 2)))
        p2 = PathFactory.create(geom=LineString((2, 2, 2), (2, 0, 0)))
        p3 = PathFactory.create(geom=LineString((2, 0, 0), (4, 0, 0)))

        # Type Point
        t = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t, path=p1,
                                      start_position=0.5, end_position=0.5)
        t = Topology.objects.get(pk=t.pk)
        self.assertEqual(t.geom, Point((1, 1, 1)))

        # 50% of path p1, 100% of path p2
        t = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t, path=p1,
                                      start_position=0.5)
        PathAggregationFactory.create(topo_object=t, path=p2)
        t = Topology.objects.get(pk=t.pk)
        self.assertEqual(t.geom, LineString((1, 1, 1), (2, 2, 2), (2, 0, 0)))

        # 100% of path p2 and p3, with offset of 1
        t = TopologyFactory.create(no_path=True, offset=1)
        PathAggregationFactory.create(topo_object=t, path=p2)
        PathAggregationFactory.create(topo_object=t, path=p3)
        t.save()
        self.assertEqual(t.geom, LineString((3, 2, 2), (3, 1, 0), (4, 1, 0)))

        # Change offset, geometry is computed again
        t.offset = 0.5
        t.save()
        self.assertEqual(t.geom, LineString((2.5, 2, 2), (2.5, 0.5, 0), (4, 0.5, 0)))
Example #13
0
    def test_spoon_loop(self):
        """
                            =====<====
                           ||       ||
        +-------===<===>===+=====>===
        """
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (10, 0, 0)))
        p2 = PathFactory.create(geom=LineString((10, 0, 0), (10, 5, 0),
                                                (20, 5, 0), (20, 0, 0),
                                                (10, 0, 0)))
        topo = TopologyFactory.create(no_path=True)
        topo.add_path(p1, start=0.3, end=1)
        topo.add_path(p2, start=1, end=0.4)
        topo.add_path(p2, start=0.4, end=0.4)
        topo.add_path(p2, start=0.4, end=0.2)
        topo.add_path(p2, start=0.2, end=0.2)
        topo.add_path(p2, start=0.2, end=0)
        topo.add_path(p1, start=1, end=0.3)
        topo.save()
        self.assertEqual(topo.geom, LineString((3, 0, 0), (10, 0, 0), (20, 0, 0), (20, 5, 0),
                                               (17, 5, 0), (11, 5, 0),  # extra point due middle aggregation
                                               (10, 5, 0), (10, 0, 0), (3, 0, 0)))

        # Deserializing should work too
        topod = Topology.deserialize("""
           [{"positions":{"0":[0.3,1],"1":[1, 0.4]},"paths":[%(pk1)s,%(pk2)s]},
            {"positions":{"0":[0.4, 0.2]},"paths":[%(pk2)s]},
            {"positions":{"0":[0.2,0],"1":[1,0.3]},"paths":[%(pk2)s,%(pk1)s]}]""" % {'pk1': p1.pk, 'pk2': p2.pk})
        self.assertEqual(topo.geom, topod.geom)
        self.assertEqual(len(topod.aggregations.all()), 7)
Example #14
0
    def test_spoon_loop_2(self):
        """
                            =====>====
                           ||       ||
        +-------===<===>===+=====<===
        """
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (10, 0, 0)))
        p2 = PathFactory.create(geom=LineString((10, 0, 0), (10, 5, 0),
                                                (20, 5, 0), (20, 0, 0),
                                                (10, 0, 0)))
        topo = TopologyFactory.create(no_path=True)
        topo.add_path(p1, start=0.3, end=1)
        topo.add_path(p2, start=0, end=0.4)
        topo.add_path(p2, start=0.4, end=0.4)
        topo.add_path(p2, start=0.4, end=0.8)
        topo.add_path(p2, start=0.8, end=0.8)
        topo.add_path(p2, start=0.8, end=1.0)
        topo.add_path(p1, start=1, end=0.3)
        topo.save()
        self.assertEqual(topo.geom, LineString((3, 0, 0), (10, 0, 0), (10, 5, 0),
                                               (17, 5, 0), (20, 5, 0),  # extra point due middle aggregation
                                               (20, 0, 0), (16, 0, 0), (10, 0, 0), (3, 0, 0)))

        # De/Serializing should work too
        serialized = """
           [{"kind": "TOPOLOGY","positions":{"0":[0.3,1],"1":[0, 0.4]},"paths":[%(pk1)s,%(pk2)s],"offset": 0.0},
            {"kind": "TOPOLOGY","positions":{"0":[0.4, 0.8]},"paths":[%(pk2)s],"offset": 0.0},
            {"kind": "TOPOLOGY","positions":{"0":[0.8,1],"1":[1,0.3]},"paths":[%(pk2)s,%(pk1)s],"offset": 0.0}]""" % {'pk1': p1.pk, 'pk2': p2.pk}

        self.assertEqual(json.loads(serialized), json.loads(topo.serialize()))
        topod = Topology.deserialize(serialized)
        self.assertEqual(topo.geom, topod.geom)
        self.assertEqual(len(topod.aggregations.all()), 7)
Example #15
0
    def test_upload(self):
        path = PathFactory(length=1)
        response = self.client.get(path.get_update_url())

        f = get_dummy_uploaded_image()
        data = {"filetype": FileTypeFactory().pk, "title": "title1", "legend": "legend1", "attachment_file": f}

        response = self.client.post(add_url_for_obj(path), data=data)
        self.assertEquals(response.status_code, 302)

        att = Attachment.objects.attachments_for_object(path).get()
        self.assertEqual(att.title, data["title"])
        self.assertEqual(att.legend, data["legend"])
        self.assertEqual(att.filetype.pk, data["filetype"])

        f.open()
        self.assertEqual(att.attachment_file.readlines(), f.readlines())

        # Check that if title is given, filename is title
        self.assertTrue(att.attachment_file.name, "title1")

        # Check that if no title is give, filename is original name
        data["title"] = ""
        response = self.client.post(add_url_for_obj(path), data=data)
        att = Attachment.objects.attachments_for_object(path)[0]
        self.assertTrue(att.attachment_file.name, "image")
Example #16
0
 def test_show_delete_multiple_path_in_list(self):
     path_1 = PathFactory.create(name="path_1", geom=LineString((0, 0), (4, 0)))
     PathFactory.create(name="path_2", geom=LineString((2, 2), (2, -2)))
     poi = POIFactory.create(no_path=True)
     poi.add_path(path_1, start=0, end=0)
     response = self.client.get(reverse('core:path_list'))
     self.assertIn('<a href="#delete" id="btn-delete" role="button">', response.content)
Example #17
0
    def test_split_tee_1(self):
        """
                C
        A +-----X----+ B
                |
                +    AB exists with topology at C.
                D    Add CD.
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        topology = TopologyFactory.create(no_path=True)
        topology.add_path(ab, start=0.5, end=0.5)
        self.assertEqual(len(topology.paths.all()), 1)

        cd = PathFactory.create(geom=LineString((2, 0, 0), (2, 2, 0)))
        cb = Path.objects.filter(name="AB").exclude(pk=ab.pk)[0]

        self.assertEqual(len(topology.paths.all()), 3)
        self.assertEqual(len(ab.aggregations.all()), 1)
        aggr_ab = ab.aggregations.all()[0]
        self.assertEqual(len(cb.aggregations.all()), 1)
        aggr_cb = cb.aggregations.all()[0]
        self.assertEqual(len(cd.aggregations.all()), 1)
        aggr_cd = cd.aggregations.all()[0]
        self.assertEqual((1.0, 1.0), (aggr_ab.start_position, aggr_ab.end_position))
        self.assertEqual((0.0, 0.0), (aggr_cb.start_position, aggr_cb.end_position))
        self.assertEqual((0.0, 0.0), (aggr_cd.start_position, aggr_cd.end_position))
Example #18
0
    def test_split_on_update(self):
        """                               + E
                                          :
                                         ||
        A +-----------+ B         A +----++---+ B
                                         ||
        C +-====-+ D              C +--===+ D
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        cd = PathFactory.create(name="CD", geom=LineString((0, -1, 0), (4, -1, 0)))
        # Create a topology
        topology = TopologyFactory.create(no_path=True)
        topology.add_path(cd, start=0.3, end=0.9)
        self.assertEqual(len(topology.paths.all()), 1)

        cd.geom = LineString((0, -1, 0), (2, -1, 0), (2, 2, 0))
        cd.save()
        cd2 = Path.objects.filter(name="CD").exclude(pk=cd.pk)[0]
        self.assertEqual(len(topology.paths.all()), 2)
        self.assertEqual(len(cd.aggregations.all()), 1)
        self.assertEqual(len(cd2.aggregations.all()), 1)
        aggr_cd = cd.aggregations.all()[0]
        aggr_cd2 = cd2.aggregations.all()[0]
        self.assertEqual((0.5, 1.0), (aggr_cd.start_position, aggr_cd.end_position))
        self.assertEqual((0.0, 0.75), (aggr_cd2.start_position, aggr_cd2.end_position))
Example #19
0
 def test_delete_multiple_path(self):
     path_1 = PathFactory.create(name="path_1", geom=LineString((0, 0), (4, 0)))
     path_2 = PathFactory.create(name="path_2", geom=LineString((2, 2), (2, -2)))
     poi = POIFactory.create(no_path=True, name="POI_1")
     poi.add_path(path_1, start=0, end=0)
     infrastructure = InfrastructureFactory.create(no_path=True, name="INFRA_1")
     infrastructure.add_path(path_1, start=0, end=1)
     signage = SignageFactory.create(no_path=True, name="SIGNA_1")
     signage.add_path(path_1, start=0, end=1)
     trail = TrailFactory.create(no_path=True, name="TRAIL_1")
     trail.add_path(path_2, start=0, end=1)
     service = ServiceFactory.create(no_path=True)
     service.add_path(path_2, start=0, end=1)
     InterventionFactory.create(topology=signage, name="INTER_1")
     response = self.client.get(reverse('core:multiple_path_delete', args=['%s,%s' % (path_1.pk, path_2.pk)]))
     self.assertIn("POI_1", response.content)
     self.assertIn("INFRA_1", response.content)
     self.assertIn("SIGNA_1", response.content)
     self.assertIn("TRAIL_1", response.content)
     self.assertIn("ServiceType", response.content)
     self.assertIn("INTER_1", response.content)
     response = self.client.post(reverse('core:multiple_path_delete', args=['%s,%s' % (path_1.pk, path_2.pk)]))
     self.assertEqual(response.status_code, 302)
     self.assertEqual(Path.objects.count(), 2)
     self.assertEqual(Path.objects.filter(pk__in=[path_1.pk, path_2.pk]).count(), 0)
Example #20
0
    def test_split_tee_3(self):
        """
                    C
        A +--=====--+---+ B
             A'  B' |
                    +    AB exists with topology A'B'.
                    D    Add CD
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        # Create a topology
        topology = TopologyFactory.create(no_path=True)
        topology.add_path(ab, start=0.3, end=0.6)
        topogeom = topology.geom
        # Topology covers 1 path
        self.assertEqual(len(ab.aggregations.all()), 1)
        self.assertEqual(len(topology.paths.all()), 1)
        self.assertEqual(topology.paths.all()[0], ab)
        cd = PathFactory.create(geom=LineString((3, 0, 0), (3, 2, 0)))
        cb = Path.objects.filter(name="AB").exclude(pk=ab.pk)[0]

        # CB does not have any
        self.assertEqual(len(cb.aggregations.all()), 0)

        # AB has still its topology
        self.assertEqual(len(ab.aggregations.all()), 1)
        # But start/end have changed
        aggr_ab = ab.aggregations.all()[0]
        self.assertEqual((0.4, 0.8), (aggr_ab.start_position, aggr_ab.end_position))
        topology.reload()
        self.assertEqual(topology.geom, topogeom)
Example #21
0
    def test_split_twice(self):
        """

             C   D
             +   +
             |   |
      A +--==+===+==--+ B
             |   |
             +---+
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        # Create a topology
        topology = TopologyFactory.create(no_path=True)
        topology.add_path(ab, start=0.1, end=0.9)
        topogeom = topology.geom
        self.assertEqual(len(topology.paths.all()), 1)
        cd = PathFactory.create(name="CD", geom=LineString((1, 2, 0), (1, -2, 0),
                                                           (3, -2, 0), (3, 2, 0)))
        self.assertEqual(len(topology.paths.all()), 3)
        self.assertEqual(len(ab.aggregations.all()), 1)
        aggr_ab = ab.aggregations.all()[0]
        self.assertEqual((0.4, 1.0), (aggr_ab.start_position, aggr_ab.end_position))
        ab2 = Path.objects.filter(name="AB").exclude(pk=ab.pk)[0]
        ab3 = Path.objects.filter(name="AB").exclude(pk__in=[ab.pk, ab2.pk])[0]
        aggr_ab2 = ab2.aggregations.all()[0]
        aggr_ab3 = ab3.aggregations.all()[0]
        self.assertEqual((0.0, 1.0), (aggr_ab2.start_position, aggr_ab2.end_position))
        self.assertEqual((0.0, 0.6), (aggr_ab3.start_position, aggr_ab3.end_position))
        topology.reload()
        self.assertNotEqual(topology.geom, topogeom)
        self.assertEqual(topology.geom.coords[0], topogeom.coords[0])
        self.assertEqual(topology.geom.coords[-1], topogeom.coords[-1])
Example #22
0
 def test_merge_works(self):
     self.login()
     p1 = PathFactory.create(name="AB", geom=LineString((0, 0), (1, 0)))
     p2 = PathFactory.create(name="BC", geom=LineString((1, 0), (2, 0)))
     response = self.client.post(reverse('core:merge_path'), {'path[]': [p1.pk, p2.pk]})
     self.assertIn('success', response.json())
     self.logout()
Example #23
0
 def test_delete_show_topologies(self):
     self.login()
     path = PathFactory(name="PATH_AB", geom=LineString((0, 0), (4, 0)))
     poi = POIFactory.create(name='POI', no_path=True)
     poi.add_path(path, start=0.5, end=0.5)
     trail = TrailFactory.create(name='Trail', no_path=True)
     trail.add_path(path, start=0.1, end=0.2)
     trek = TrekFactory.create(name='Trek', no_path=True)
     trek.add_path(path, start=0.2, end=0.3)
     service = ServiceFactory.create(no_path=True, type__name='ServiceType')
     service.add_path(path, start=0.2, end=0.3)
     signage = SignageFactory.create(name='Signage', no_path=True)
     signage.add_path(path, start=0.2, end=0.2)
     infrastructure = InfrastructureFactory.create(name='Infrastructure', no_path=True)
     infrastructure.add_path(path, start=0.2, end=0.2)
     intervention1 = InterventionFactory.create(topology=signage, name='Intervention1')
     t = TopologyFactory.create(no_path=True)
     t.add_path(path, start=0.2, end=0.5)
     intervention2 = InterventionFactory.create(topology=t, name='Intervention2')
     response = self.client.get(path.get_delete_url())
     self.assertEqual(response.status_code, 200)
     self.assertContains(response, 'Different topologies are linked with this path')
     self.assertContains(response, '<a href="/poi/%d/">POI</a>' % poi.pk)
     self.assertContains(response, '<a href="/trail/%d/">Trail</a>' % trail.pk)
     self.assertContains(response, '<a href="/trek/%d/">Trek</a>' % trek.pk)
     self.assertContains(response, '<a href="/service/%d/">ServiceType</a>' % service.pk)
     self.assertContains(response, '<a href="/signage/%d/">Signage</a>' % signage.pk)
     self.assertContains(response, '<a href="/infrastructure/%d/">Infrastructure</a>' % infrastructure.pk)
     self.assertContains(response, '<a href="/intervention/%d/">Intervention1</a>' % intervention1.pk)
     self.assertContains(response, '<a href="/intervention/%d/">Intervention2</a>' % intervention2.pk)
Example #24
0
    def test_split_twice(self):
        """

             C   D
             +   +
             |   |
        A +--+---+--+ B
             |   |
             +---+

        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0, 0), (4, 0, 0)))
        cd = PathFactory.create(name="CD", geom=LineString((1, 2, 0), (1, -2, 0),
                                                           (3, -2, 0), (3, 2, 0)))
        ab.reload()
        self.assertEqual(ab.length, 1)
        self.assertEqual(cd.length, 2)
        ab_clones = Path.objects.filter(name="AB").exclude(pk=ab.pk)
        cd_clones = Path.objects.filter(name="CD").exclude(pk=cd.pk)
        self.assertEqual(len(ab_clones), 2)
        self.assertEqual(len(cd_clones), 2)
        self.assertEqual(ab_clones[0].geom, LineString((1, 0, 0), (3, 0, 0)))
        self.assertEqual(ab_clones[1].geom, LineString((3, 0, 0), (4, 0, 0)))

        self.assertEqual(cd_clones[0].geom, LineString((1, 0, 0), (1, -2, 0),
                                                       (3, -2, 0), (3, 0, 0)))
        self.assertEqual(cd_clones[1].geom, LineString((3, 0, 0), (3, 2, 0)))
Example #25
0
 def test_return_path_serialized(self):
     """
     Same as test_return_path() but from deserialization.
     """
     p1 = PathFactory.create(geom=LineString((0, 0), (10, 0)))
     p2 = PathFactory.create(geom=LineString((5, 0), (5, 10), (10, 10)))
     p3 = Path.objects.filter(name=p1.name).exclude(pk=p1.pk)[0]  # Was splitted :)
     topo = Topology.deserialize(
         """
        [{"offset":0,
          "positions":{"0":[0.5,1],
                       "1":[0.0, 0.8]},
          "paths":[%(p1)s,%(p2)s]
         },
         {"offset":0,
          "positions":{"0":[0.8,0.0],
                       "1":[0.0, 0.5]},
          "paths":[%(p2)s,%(p3)s]
         }
        ]
     """
         % {"p1": p1.pk, "p2": p2.pk, "p3": p3.pk}
     )
     topo.save()
     self.assertEqual(topo.geom, LineString((2.5, 0), (5, 0), (5, 10), (7, 10), (5, 10), (5, 0), (7.5, 0)))
Example #26
0
 def test_delete_view_multiple_path(self):
     path_1 = PathFactory.create(name="path_1", geom=LineString((0, 0), (4, 0)))
     path_2 = PathFactory.create(name="path_2", geom=LineString((2, 2), (2, -2)))
     poi = POIFactory.create(no_path=True)
     poi.add_path(path_1, start=0, end=0)
     response = self.client.get(reverse('core:multiple_path_delete', args=['%s,%s' % (path_1.pk, path_2.pk)]))
     self.assertEqual(response.status_code, 200)
     self.assertIn('Do you really wish to delete', response.content)
Example #27
0
    def test_merge_fails_donttouch(self):
        self.login()
        p3 = PathFactory.create(name="AB", geom=LineString((0, 0), (1, 0)))
        p4 = PathFactory.create(name="BC", geom=LineString((500, 0), (1000, 0)))

        response = self.client.post(reverse('core:merge_path'), {'path[]': [p3.pk, p4.pk]})
        self.assertIn('error', response.json())
        self.logout()
Example #28
0
 def get_good_data(self):
     PathFactory.create()
     return {
         'name': 'test',
         'description': 'oh',
         'structure': default_structure().pk,
         'type': InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk,
         'topology': '{"lat": 0.42, "lng": 0.666}'
     }
Example #29
0
 def get_good_data(self):
     PathFactory.create()
     return {
         "name": "test",
         "description": "oh",
         "structure": default_structure().pk,
         "type": InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk,
         "topology": '{"lat": 0.42, "lng": 0.666}',
     }
Example #30
0
 def get_good_data(self):
     PathFactory.create()
     return {
         'name_fr': 'test',
         'name_en': 'test',
         'description_fr': 'ici',
         'description_en': 'here',
         'type': POITypeFactory.create().pk,
         'topology': '{"lat": 5.1, "lng": 6.6}'
     }
Example #31
0
    def test_detail_target_objects(self):
        self.login()
        if settings.TREKKING_TOPOLOGY_ENABLED:
            path = PathFactory.create(geom=LineString((200, 200), (300, 300)))
            signa = SignageFactory.create(paths=[(path, .5, .5)])
            signa.save()
            infrastructure = InfrastructureFactory.create(paths=[(path, .5, .5)])
            infrastructure.save()
            poi = POIFactory.create(paths=[(path, .5, .5)])
            trek = TrekFactory.create(paths=[(path, .5, .5)])
            service = ServiceFactory.create(paths=[(path, .5, .5)])
            topo = TopologyFactory.create(paths=[(path, .5, .5)])
            topo.save()

            path_other = PathFactory.create(geom=LineString((10000, 0), (10010, 0)))
            signa_other = SignageFactory.create(paths=[(path_other, .5, .5)])
            signa_other.save()
        else:
            signa = SignageFactory.create(geom='SRID=2154;POINT (250 250)')
            infrastructure = InfrastructureFactory.create(geom='SRID=2154;POINT (250 250)')
            poi = POIFactory.create(geom='SRID=2154;POINT (250 250)')
            trek = TrekFactory.create(geom='SRID=2154;POINT (250 250)')
            service = ServiceFactory.create(geom='SRID=2154;POINT (250 250)')
            topo = TopologyFactory.create(geom='SRID=2154;POINT (250 250)')

            signa_other = SignageFactory.create(geom='SRID=2154;POINT (10005 0)')

        intervention_signa = InterventionFactory.create(target=signa)
        intervention_infra = InterventionFactory.create(target=infrastructure)
        intervention_poi = InterventionFactory.create(target=poi)
        intervention_trek = InterventionFactory.create(target=trek)
        intervention_service = InterventionFactory.create(target=service)
        intervention_topo = InterventionFactory.create(target=topo)
        blade = BladeFactory(signage=signa, number="1")
        intervention_blade = InterventionFactory.create(target=blade)

        intervention_other = InterventionFactory.create(target=signa_other)

        response = self.client.get(signa.get_detail_url())
        self.assertEqual(response.status_code, 200)

        self.assertContains(response, intervention_signa.target_display)
        self.assertContains(response, intervention_infra.target_display)
        self.assertContains(response, intervention_poi.target_display)
        self.assertContains(response, intervention_trek.target_display)
        self.assertContains(response, intervention_service.target_display)
        self.assertContains(response, intervention_blade.target_display)
        self.assertContains(response, intervention_topo.target_display)

        self.assertNotContains(response, intervention_other.target_display)
Example #32
0
 def test_serializer_model_geofield_do_not_exist(self):
     app_settings['GEOM_FIELD_NAME'] = 'do_not_exist'
     self.serializer = ZipShapeSerializer()
     PathFactory.create()
     response = HttpResponse()
     with self.assertRaisesRegex(
             ValueError,
             "Geodjango geometry field not found with the name 'do_not_exist', "
             "fields available are: 'geom_3d, geom, geom_cadastre'"):
         self.serializer.serialize(Path.objects.all(),
                                   stream=response,
                                   fields=['id', 'name'],
                                   delete=False)
     app_settings['GEOM_FIELD_NAME'] = 'geom'
Example #33
0
 def test_form_default_stake(self):
     self.login()
     good_data = self.get_good_data()
     good_data['stake'] = ''
     good_data['topology'] = """
     {"offset":0,"positions":{"0":[0.8298653170816073,1],"2":[0,0.04593024777973237]},"paths":[%s,%s,%s]}
     """ % (PathFactory.create().pk, PathFactory.create().pk,
            PathFactory.create().pk)
     response = self.client.post(Intervention.get_add_url(), good_data)
     self.assertEqual(response.status_code, 302)
     response = self.client.get(response._headers['location'][1])
     self.assertTrue('object' in response.context)
     intervention = response.context['object']
     self.assertFalse(intervention.stake is None)
Example #34
0
    def test_dates(self):
        t1 = dbnow()
        p = PathFactory()
        t2 = dbnow()
        self.assertTrue(t1 < p.date_insert < t2,
                        msg='Date interval failed: %s < %s < %s' %
                        (t1, p.date_insert, t2))

        p.name = "Foo"
        p.save()
        t3 = dbnow()
        self.assertTrue(t2 < p.date_update < t3,
                        msg='Date interval failed: %s < %s < %s' %
                        (t2, p.date_update, t3))
Example #35
0
    def test_troncons_link(self):
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (1, 1, 1)))
        p2 = PathFactory.create(geom=LineString((1, 1, 1), (3, 3, 3)))
        p3 = PathFactory.create(geom=LineString((3, 3, 3), (4, 4, 4)))
        p4 = PathFactory.create(
            geom=LineString((4, 1, 1), (6, 2, 2), (4, 3, 3)))

        # Paths should not be linked to anything at this stage
        self.assertEquals(p1.aggregations.count(), 0)
        self.assertEquals(p2.aggregations.count(), 0)
        self.assertEquals(p3.aggregations.count(), 0)

        c1 = City.objects.create(code='005177',
                                 name='Trifouillis-les-oies',
                                 geom=MultiPolygon(
                                     Polygon(((0, 0), (2, 0), (2, 4), (0, 4),
                                              (0, 0)),
                                             srid=settings.SRID)))
        c2 = City.objects.create(code='005179',
                                 name='Trifouillis-les-poules',
                                 geom=MultiPolygon(
                                     Polygon(((2, 0), (5, 0), (5, 4), (2, 4),
                                              (2, 0)),
                                             srid=settings.SRID)))

        # There should be automatic link after insert
        self.assertEquals(p1.aggregations.count(), 1)
        self.assertEquals(p2.aggregations.count(), 2)
        self.assertEquals(p3.aggregations.count(), 1)
        self.assertEquals(p4.aggregations.count(), 2)

        c1.geom = MultiPolygon(
            Polygon(((1.5, 0), (2, 0), (2, 4), (1.5, 4), (1.5, 0)),
                    srid=settings.SRID))
        c1.save()

        # Links should have been updated after geom update
        self.assertEquals(p1.aggregations.count(), 0)
        self.assertEquals(p2.aggregations.count(), 2)
        self.assertEquals(p3.aggregations.count(), 1)
        self.assertEquals(p4.aggregations.count(), 2)

        c1.delete()

        # Links should have been updated after delete
        self.assertEquals(p1.aggregations.count(), 0)
        self.assertEquals(p2.aggregations.count(), 1)
        self.assertEquals(p3.aggregations.count(), 1)
        self.assertEquals(p4.aggregations.count(), 2)
Example #36
0
 def test_deserialize_point(self):
     PathFactory.create()
     # Take a point
     p = Point(2, 1, 0, srid=settings.SRID)
     p.transform(settings.API_SRID)
     closest = Path.closest(p)
     # Check closest path
     self.assertEqual(closest.geom.coords, ((1.0, 1.0, 0.0), (2.0, 2.0, 0.0)))
     # The point has same x as first point of path, and y to 0 :
     topology = Topology.deserialize('{"lng": %s, "lat": %s}' % (p.x, p.y))
     self.assertAlmostEqual(topology.offset, -0.7071, 3)
     self.assertEqual(len(topology.paths.all()), 1)
     pagg = topology.aggregations.get()
     self.assertTrue(almostequal(pagg.start_position, 0.5))
     self.assertTrue(almostequal(pagg.end_position, 0.5))
    def setUpTestData(cls):
        p1 = PathFactory()
        cls.seek_path = PathFactory(geom=getRandomLineStringInBounds())

        topo_1 = TopologyFactory.create(paths=[p1])
        seek_topo = TopologyFactory.create(paths=[cls.seek_path])

        it_p1 = InterventionFactory.create(target=topo_1)
        seek_it = InterventionFactory.create(target=seek_topo)

        cls.seek_proj = ProjectFactory.create()
        cls.seek_proj.interventions.add(seek_it)

        proj_1 = ProjectFactory.create()
        proj_1.interventions.add(it_p1)
 def test_serializer_model_geofield_multiple(self):
     app_settings['GEOM_FIELD_NAME'] = None
     self.serializer = ZipShapeSerializer()
     PathFactory.create()
     response = HttpResponse()
     with self.assertRaisesRegex(
             ValueError,
             "More than one geodjango geometry field found, please specify which "
             "to use by name using the 'geo_field' keyword. "
             "Available fields are: 'geom_3d, geom, geom_cadastre'"):
         self.serializer.serialize(Path.objects.all(),
                                   stream=response,
                                   fields=['id', 'name'],
                                   delete=False)
     app_settings['GEOM_FIELD_NAME'] = 'geom'
Example #39
0
    def test_pois_should_be_ordered_by_progression(self):
        p1 = PathFactory.create(geom=LineString((0, 0), (4, 4)))
        p2 = PathFactory.create(geom=LineString((4, 4), (8, 8)))
        self.trek = TrekFactory.create(paths=[p1, p2])

        self.trek_reverse = TrekFactory.create(paths=[(p2, 0.8, 0), (p1, 1, 0.2)])

        self.poi1 = POIFactory.create(paths=[(p1, 0.8, 0.8)])
        self.poi2 = POIFactory.create(paths=[(p1, 0.3, 0.3)])
        self.poi3 = POIFactory.create(paths=[(p2, 0.5, 0.5)])

        pois = self.trek.pois
        self.assertEqual([self.poi2, self.poi1, self.poi3], list(pois))
        pois = self.trek_reverse.pois
        self.assertEqual([self.poi3, self.poi1, self.poi2], list(pois))
    def test_merge_fails_parameters(self):
        """
        Should fail if path[] length != 2
        """
        self.login()
        p1 = PathFactory.create()
        p2 = PathFactory.create()
        response = self.client.post(reverse('core:merge_path'),
                                    {'path[]': [p1.pk]})
        self.assertIn('error', response.json())

        response = self.client.post(reverse('core:merge_path'),
                                    {'path[]': [p1.pk, p1.pk, p2.pk]})
        self.assertIn('error', response.json())
        self.logout()
Example #41
0
 def get_good_data(self):
     PathFactory.create()
     return {
         'name':
         'test',
         'description':
         'oh',
         'type':
         InfrastructureTypeFactory.create(
             type=INFRASTRUCTURE_TYPES.BUILDING).pk,
         'condition':
         InfrastructureConditionFactory.create().pk,
         'topology':
         '{"lat": 0.42, "lng": 0.666}'
     }
    def test_recompute_pk_no_reverse(self):
        """
        A---------------B + C-------------------D         A----------------BC----------------D
          |        |          |--|           |         =>   |       |        |--|           |
          E1 (0.2) |          E3 (0.2, 0.3)  |             E1 (0.1) |        E3 (0.6, 0.65) E4 (0.9)
                   E2 (0.6)                  E4 (0.8)               E2 (0.3)

        In case of AB == CD, matching B and C
        """
        path_AB = PathFactory.create(name="PATH_AB", geom=LineString((0, 1), (10, 1)))
        path_CD = PathFactory.create(name="PATH_CD", geom=LineString((10, 1), (20, 1)))

        e1 = TopologyFactory.create(geom=Point(2, 2))
        a1 = PathAggregationFactory.create(path=path_AB, topo_object=e1)

        e2 = TopologyFactory.create(geom=Point(6, 1))
        a2 = PathAggregationFactory.create(path=path_AB, topo_object=e2)

        e3 = TopologyFactory.create(geom=LineString((2, 1), (3, 1),))
        a3 = PathAggregationFactory.create(path=path_CD, topo_object=e3)
        e4 = TopologyFactory.create(geom=Point(8, 2))
        a4 = PathAggregationFactory.create(path=path_CD, topo_object=e4)

        path_AB_original_length = path_AB.length
        path_CD_original_length = path_CD.length
        path_AB.merge_path(path_CD)

        self.assertEqual(path_AB.geom, LineString((0, 1), (10, 1), (20, 1), srid=settings.SRID))

        # reload updated objects
        a1_updated = PathAggregation.objects.get(pk=a1.pk)
        a2_updated = PathAggregation.objects.get(pk=a2.pk)
        a3_updated = PathAggregation.objects.get(pk=a3.pk)
        a4_updated = PathAggregation.objects.get(pk=a4.pk)

        # test pk recompute on path_1 : new pk = old pk * old_path_1_length / new_path_1_length
        self.assertEqual(a1_updated.start_position, a1.start_position * (path_AB_original_length / path_AB.length))
        self.assertEqual(a1_updated.end_position, a1.end_position * (path_AB_original_length / path_AB.length))

        self.assertEqual(a2_updated.start_position, a2.start_position * (path_AB_original_length / path_AB.length))
        self.assertEqual(a2_updated.end_position, a1.end_position * (path_AB_original_length / path_AB.length))

        # test pk recompute on path_2 : new pk = old pk * old_path_2_length / new_path_1_length + old_path_1_length / new_path_1_length
        self.assertEqual(a3_updated.start_position, a3.start_position * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)
        self.assertEqual(a3_updated.end_position, a3.end_position * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)

        self.assertEqual(a4_updated.start_position, a4.start_position * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)
        self.assertEqual(a4_updated.end_position, a4.end_position * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)
 def test_extent(self):
     p1 = PathFactory.create()
     lng_min, lat_min, lng_max, lat_max = p1.extent
     self.assertAlmostEqual(lng_min, 3.0)
     self.assertAlmostEqual(lat_min, 46.499999999999936)
     self.assertAlmostEqual(lng_max, 3.0013039767202154)
     self.assertAlmostEqual(lat_max, 46.50090044234927)
Example #44
0
    def test_opposite_paths(self):
        """
                A  C
        B +-------+-------+ D

        """
        ab = PathFactory.create(geom=LineString((5, 0), (0, 0)))
        cd = PathFactory.create(geom=LineString((5, 0), (10, 0)))
        topo = TopologyFactory.create(paths=[(ab, 0.2, 0), (cd, 0, 0.2)])
        expected = LineString((4, 0), (5, 0), (6, 0), srid=settings.SRID)
        self.assertEqual(topo.geom, expected)
        # Now let's have some fun, reverse BA :)
        ab.reverse()
        ab.save()
        topo.reload()
        self.assertEqual(topo.geom, expected)
Example #45
0
    def test_trek_all_reverse(self):
        """

        +----<===+=======+====|----->

        """
        p1 = PathFactory.create(geom=LineString((0, 0), (10, 0)))
        p2 = PathFactory.create(geom=LineString((10, 0), (20, 0)))
        p3 = PathFactory.create(geom=LineString((20, 0), (30, 0)))

        topo = TopologyFactory.create(paths=[(p3, 0.2, 0), (p2, 1,
                                                            0), (p1, 1, 0.9)])
        self.assertEqual(
            topo.geom,
            LineString((22.0, 0.0), (20.0, 0.0), (10.0, 0.0), (9.0, 0.0),
                       srid=settings.SRID))
Example #46
0
 def test_point_geom_not_moving(self):
     r"""
     Modify path, point not moving
     +                  +
     |                  |
      \     X          /        X
      /                \
     |                  |
     +                  +
     """
     p1 = PathFactory.create(
         geom=LineString((0, 0), (0, 5), (5, 10), (0, 15), (0, 20)))
     poi = Point(10, 10, srid=settings.SRID)
     poi.transform(settings.API_SRID)
     poitopo = Topology.deserialize({'lat': poi.y, 'lng': poi.x})
     self.assertEqual(0.5, poitopo.aggregations.all()[0].start_position)
     self.assertAlmostEqual(-5, poitopo.offset, places=6)
     # It should have kept its position !
     self.assertAlmostEqual(10, poitopo.geom.x, places=6)
     self.assertAlmostEqual(10, poitopo.geom.y, places=6)
     # Change path, it should still be in the same position
     p1.geom = LineString((0, 0), (0, 5), (-5, 10), (0, 15), (0, 20))
     p1.save()
     poitopo.reload()
     self.assertAlmostEqual(10, poitopo.geom.x, places=6)
     self.assertAlmostEqual(10, poitopo.geom.y, places=6)
Example #47
0
    def test_point_offset_updated(self):
        """
        Shorten path, offset updated.

               X                              X
        +-----------+            +------+

        """
        p1 = PathFactory.create(geom=LineString((0, 0), (20, 0)))
        poi = Point(10, 10, srid=settings.SRID)
        poi.transform(settings.API_SRID)
        poitopo = Topology.deserialize({'lat': poi.y, 'lng': poi.x})
        poitopo.save()
        self.assertAlmostEqual(0.5,
                               poitopo.aggregations.all()[0].start_position,
                               places=6)
        self.assertAlmostEqual(10, poitopo.offset, places=6)

        p1.geom = LineString((0, 0), (0, 5))
        p1.save()
        poitopo.reload()
        self.assertAlmostEqual(11.180339887, poitopo.offset, places=6)
        # Not moved:
        self.assertAlmostEqual(10, poitopo.geom.x, places=6)
        self.assertAlmostEqual(10, poitopo.geom.y, places=6)
    def test_path_helpers(self):
        p = PathFactory.create()

        self.assertEqual(len(p.interventions), 0)
        self.assertEqual(len(p.projects), 0)

        sign = SignageFactory.create(no_path=True)
        sign.add_path(p, start=0.5, end=0.5)

        infra = InfrastructureFactory.create(no_path=True)
        infra.add_path(p)

        i1 = InterventionFactory.create()
        i1.set_topology(sign)
        i1.save()

        self.assertCountEqual(p.interventions, [i1])

        i2 = InterventionFactory.create()
        i2.set_topology(infra)
        i2.save()

        self.assertCountEqual(p.interventions, [i1, i2])

        proj = ProjectFactory.create()
        proj.interventions.add(i1)
        proj.interventions.add(i2)

        self.assertCountEqual(p.projects, [proj])
Example #49
0
    def test_path_merge_with_other_path_next_ws(self):
        """
                          F
                          |
                          |
                          E
        A---------------B + C-------------------D

        Do not merge !
        """
        path_AB = PathFactory.create(name="PATH_AB",
                                     geom=LineString((0, 1), (10, 1)))
        path_CD = PathFactory.create(name="PATH_CD",
                                     geom=LineString((10, 1), (20, 1)))
        PathFactory.create(name="PATH_EF", geom=LineString((10, 1), (10, 5)))
        self.assertEqual(path_AB.merge_path(path_CD), 2)
Example #50
0
 def create_infrastructure_point_intervention(obj, create, extracted,
                                              **kwargs):
     infra = InfrastructureFactory.create(no_path=True)
     infra.add_path(PathFactory.create(), start=0.5, end=0.5)
     obj.set_infrastructure(infra)
     if create:
         obj.save()
Example #51
0
    def test_save_path_with_edit_draft_path_and_edit_path(self):
        """
        Check save path without permission change_path save with draft=True
        """
        draft_path = PathFactory(name="draft",
                                 geom=LineString((0, 2), (4, 2)),
                                 draft=True)

        self.user.user_permissions.add(
            Permission.objects.get(codename='change_path'))
        self.user.user_permissions.add(
            Permission.objects.get(codename='change_draft_path'))
        self.client.login(username=self.user.username, password='******')

        data = self.get_good_data()
        data['draft'] = True
        response = self.client.post('/path/edit/%s/' % draft_path.pk, data)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(Path.objects.get(pk=draft_path.pk).draft)

        # You can change a draft path to a normal path.
        data['draft'] = False
        response = self.client.post('/path/edit/%s/' % draft_path.pk, data)
        self.assertEqual(response.status_code, 302)
        self.assertFalse(Path.objects.get(pk=draft_path.pk).draft)

        # You can't change a normal path back to a draft path.
        data['draft'] = True
        response = self.client.post('/path/edit/%s/' % draft_path.pk, data)
        self.assertEqual(response.status_code, 302)
        self.assertFalse(Path.objects.get(pk=draft_path.pk).draft)
Example #52
0
    def test_tiles(self, mock_tileslist, mock_tiles):
        output = StringIO()

        p = PathFactory.create(geom=LineString((0, 0), (0, 10)))
        trek_multi = TrekFactory.create(published=True,
                                        paths=[(p, 0, 0.1), (p, 0.2, 0.3)])
        management.call_command('sync_rando',
                                os.path.join('var', 'tmp'),
                                url='http://localhost:8000',
                                verbosity=2,
                                languages='en',
                                stdout=output)
        zfile = zipfile.ZipFile(
            os.path.join('var', 'tmp', 'zip', 'tiles', 'global.zip'))
        for finfo in zfile.infolist():
            ifile_global = zfile.open(finfo)
            if ifile_global.name.startswith('tiles/'):
                self.assertEqual(ifile_global.readline(), b'I am a png')
        zfile_trek = zipfile.ZipFile(
            os.path.join('var', 'tmp', 'zip', 'tiles',
                         '{}.zip'.format(trek_multi.pk)))
        for finfo in zfile_trek.infolist():
            ifile_trek = zfile_trek.open(finfo)
            if ifile_trek.name.startswith('tiles/'):
                self.assertEqual(ifile_trek.readline(), b'I am a png')
        self.assertIn("tiles/global.zip", output.getvalue())
        self.assertIn("tiles/{pk}.zip".format(pk=trek_multi.pk),
                      output.getvalue())
Example #53
0
    def test_trek_all_reverse(self):
        """

        +----<===+=======+====|----->

        """
        p1 = PathFactory.create(geom=LineString((0, 0), (10, 0)))
        p2 = PathFactory.create(geom=LineString((10, 0), (20, 0)))
        p3 = PathFactory.create(geom=LineString((20, 0), (30, 0)))

        topo = TopologyFactory.create(no_path=True)
        topo.add_path(p3, start=0.2, end=0)
        topo.add_path(p2, start=1, end=0)
        topo.add_path(p1, start=1, end=0.9)
        topo.save()
        self.assertEqual(topo.geom, LineString((22.0, 0.0), (20.0, 0.0), (10.0, 0.0), (9.0, 0.0)))
Example #54
0
 def test_json_graph_simple(self):
     path = PathFactory(geom=LineString((0, 0), (1, 1)))
     response = self.client.get(self.url)
     self.assertEqual(response.status_code, 200)
     graph = response.json()
     self.assertDictEqual({'edges': {str(path.pk): {'id': path.pk, 'length': 1.4142135623731, 'nodes_id': [1, 2]}},
                           'nodes': {'1': {'2': path.pk}, '2': {'1': path.pk}}}, graph)
Example #55
0
 def test_deleted_pois(self):
     p1 = PathFactory.create(geom=LineString((0, 0), (4, 4)))
     trek = TrekFactory.create(paths=[p1])
     poi = POIFactory.create(paths=[(p1, 0.6, 0.6)])
     self.assertCountEqual(trek.pois, [poi])
     poi.delete()
     self.assertCountEqual(trek.pois, [])
 def test_update_infrastructure(self):
     self.login()
     target_year = 2017
     if settings.TREKKING_TOPOLOGY_ENABLED:
         intervention = InfrastructureInterventionFactory.create()
     else:
         intervention = InfrastructureInterventionFactory.create(
             geom='SRID=2154;POINT (700000 6600000)')
     infra = intervention.infrastructure
     # Save infrastructure form
     response = self.client.get(infra.get_update_url())
     form = response.context['form']
     data = form.initial
     data['name'] = 'modified'
     data['implantation_year'] = target_year
     if settings.TREKKING_TOPOLOGY_ENABLED:
         data['topology'] = '{"paths": [%s]}' % PathFactory.create().pk
     else:
         data['geom'] = 'SRID=4326;POINT (2.0 6.6)'
     response = self.client.post(infra.get_update_url(), data)
     self.assertEqual(response.status_code, 302)
     # Check that intervention was not deleted (bug #783)
     intervention.reload()
     self.assertFalse(intervention.deleted)
     self.assertEqual(intervention.infrastructure.name, 'modified')
     self.assertEqual(intervention.infrastructure.implantation_year,
                      target_year)
Example #57
0
 def get_good_data(self):
     InterventionStatusFactory.create()  # in case not any in db
     path = PathFactory.create()
     return {
         'name': 'test',
         'date': '2012-08-23',
         'structure': default_structure().pk,
         'disorders': InterventionDisorderFactory.create().pk,
         'comments': '',
         'slope': 0,
         'area': 0,
         'subcontract_cost': 0.0,
         'stake': StakeFactory.create().pk,
         'height': 0.0,
         'project': '',
         'width': 0.0,
         'length': 0.0,
         'status': InterventionStatus.objects.all()[0].pk,
         'heliport_cost': 0.0,
         'material_cost': 0.0,
         'topology': '{"paths": [%s]}' % path.pk,
         'manday_set-TOTAL_FORMS': '2',
         'manday_set-INITIAL_FORMS': '0',
         'manday_set-MAX_NUM_FORMS': '',
         'manday_set-0-nb_days': '48.75',
         'manday_set-0-job': InterventionJobFactory.create().pk,
         'manday_set-0-id': '',
         'manday_set-0-DELETE': '',
         'manday_set-1-nb_days': '12',
         'manday_set-1-job': InterventionJobFactory.create().pk,
         'manday_set-1-id': '',
         'manday_set-1-DELETE': '',
     }
Example #58
0
    def test_point_offset_kept(self):
        """
        Shorten path, offset kept.

          X                        X
        +-----------+            +------+

        """
        p1 = PathFactory.create(geom=LineString((0, 0), (20, 0)))
        poi = Point(5, 10, srid=settings.SRID)
        poi.transform(settings.API_SRID)
        poitopo = Topology.deserialize({'lat': poi.y, 'lng': poi.x})
        self.assertTrue(
            almostequal(0.25,
                        poitopo.aggregations.all()[0].start_position))
        self.assertTrue(almostequal(10, poitopo.offset))

        p1.geom = LineString((0, 0), (10, 0))
        p1.save()
        poitopo.reload()

        self.assertTrue(almostequal(10, poitopo.offset))
        # Not moved:
        self.assertTrue(almostequal(5, poitopo.geom.x))
        self.assertTrue(almostequal(10, poitopo.geom.y))
Example #59
0
 def test_json_graph_headers(self):
     """
     Last modified depends on
     """
     PathFactory(geom=LineString((0, 0), (1, 1)))
     response = self.client.get(self.url)
     self.assertNotEqual(response['Cache-Control'], None)
    def test_helpers(self):
        p = PathFactory.create()

        infra = InfrastructureFactory.create(no_path=True)
        infra.add_path(path=p)

        self.assertCountEqual(p.infrastructures, [infra])