Exemple #1
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)))
Exemple #2
0
 def test_length(self):
     e = TopologyFactory.build(no_path=True)
     self.assertEqual(e.length, 0)
     e.save()
     self.assertEqual(e.length, 0)
     PathAggregationFactory.create(topo_object=e)
     e.save()
     self.assertNotEqual(e.length, 0)
Exemple #3
0
 def test_path_helpers(self):
     if not self.factory:
         return   # ignore abstract test
     p = PathFactory.create()
     self.assertEquals(len(getattr(p, self.helper_name)), 0)
     l = self.factory.create(no_path=True)
     PathAggregationFactory.create(topo_object=l, path=p)
     self.assertEqual([o.pk for o in getattr(p, self.helper_name).all()],
                      [l.pk])
    def test_recompute_pk_reverse_AB_CD(self):
        """
        A---------------B + C-------------------D         B----------------AD----------------C
          |        |          |--|           |         =>    |          |     |         |--|
          E1 (0.2) |          E3 (0.2, 0.3)  |               |       E1 (0.4) |         E3 (0.8, 0.9)
                   E2 (0.6)                  E4 (0.8)       E2 (0.2)         E4 (0.6)

        In case of AB == CD, matching A and D
        """
        path_AB = PathFactory.create(name="PATH_AB", geom=LineString((10, 1), (0, 1)))
        path_CD = PathFactory.create(name="PATH_CD", geom=LineString((20, 1), (10, 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)))

        # 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, (1 - a1.start_position) * (path_AB_original_length / path_AB.length))
        self.assertEqual(a1_updated.end_position, (1 - a1.end_position) * (path_AB_original_length / path_AB.length))

        self.assertEqual(a2_updated.start_position, (1 - a2.start_position) * (path_AB_original_length / path_AB.length))
        self.assertEqual(a2_updated.end_position, (1 - 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, (1 - a3.start_position) * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)
        self.assertEqual(a3_updated.end_position, (1 - a3.end_position) * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)

        self.assertEqual(a4_updated.start_position, (1 - a4.start_position) * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)
        self.assertEqual(a4_updated.end_position, (1 - a4.end_position) * (path_CD_original_length / path_AB.length) + path_AB_original_length / path_AB.length)

        # test offset changes
        e1_updated = Topology.objects.get(pk=e1.pk)
        self.assertEqual(e1_updated.offset, -e1.offset)
        e4_updated = Topology.objects.get(pk=e4.pk)
        self.assertEqual(e4_updated.offset, -e4.offset)
Exemple #5
0
    def create_pair_of_distinct_by_topo_intervention(self):
        p1, seek_path = self.create_pair_of_distinct_path()

        topo_1 = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=topo_1, path=p1, start_position=0, end_position=1)

        seek_topo = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=seek_topo, path=seek_path, start_position=0, end_position=1)

        it_p1 = InterventionFactory.create(topology=topo_1)
        seek_it = InterventionFactory.create(topology=seek_topo)
        return seek_it, seek_path
Exemple #6
0
    def test_helpers(self):
        p = PathFactory.create()

        self.assertEquals(len(p.infrastructures), 0)
        sign = SignageFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=sign, path=p, start_position=0.5, end_position=0.5)

        self.assertItemsEqual(p.signages, [sign])

        infra = InfrastructureFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=infra, path=p)

        self.assertItemsEqual(p.infrastructures, [infra])
Exemple #7
0
    def test_elevation(self):
        trek = TrekFactory.create(no_path=True)
        p1 = PathFactory.create(geom=LineString((1, 0, 1), (0, 0, 1), (0, 1, 1)))
        p2 = PathFactory.create(geom=LineString((0, 1, 1), (1, 1, 1), (1, 2, 2)))
        PathAggregationFactory.create(topo_object=trek, path=p1,
                                      start_position=0.5)
        PathAggregationFactory.create(topo_object=trek, path=p2)

        trek = Trek.objects.get(pk=trek.pk)  # reload to get computed fields

        actual_profile = trek.elevation_profile
        expected_profile = ((0, 1), (1, 1), (2, 1), (2 + 2 ** 0.5, 2))
        self.assertItemsEqual(actual_profile, expected_profile)
Exemple #8
0
    def test_filter_by_physical_edge(self):
        seek_inter, seek_path = self.create_pair_of_distinct_by_topo_intervention()

        edge = PhysicalEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge, path=seek_path, start_position=0, end_position=1)
        edge.reload()

        # filter by physical type
        data = {'physical_type': edge.physical_type.pk}

        qs = InterventionFilterSet(data=data).qs
        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_inter)
    def test_filter_by_land_edge(self):
        seek_proj, seek_path = self.create_pair_of_distinct_by_topo_project()

        edge = LandEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge, path=seek_path, start_position=0, end_position=1)
        edge.reload()

        # filter by land type
        data = {'land_type': edge.land_type.pk}

        qs = ProjectFilterSet(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_proj)
Exemple #10
0
    def test_filter_by_competence_edge(self):
        seek_proj, seek_path = self.create_pair_of_distinct_by_topo_project()

        edge = CompetenceEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge, path=seek_path, start_position=0, end_position=1)
        edge.reload()

        # filter by organization
        data = { 'competence': edge.organization.pk }

        qs = ProjectFilter(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_proj)
Exemple #11
0
    def test_filter_by_signage_management_edge(self):
        seek_inter, seek_path = self.create_pair_of_distinct_by_topo_intervention()

        edge = SignageManagementEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge, path=seek_path, start_position=0, end_position=1)
        edge.reload()

        # filter by organization
        data = {'signage': edge.organization.pk}

        qs = InterventionFilterSet(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_inter)
Exemple #12
0
    def test_helpers(self):
        trek = TrekFactory.create(no_path=True)
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (4, 4, 2)))
        p2 = PathFactory.create(geom=LineString((4, 4, 2), (8, 8, 4)))
        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])
Exemple #13
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])
Exemple #14
0
    def test_filter_by_work_management_edge(self):
        seek_proj, seek_path = self.create_pair_of_distinct_by_topo_project()

        edge = WorkManagementEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge, path=seek_path, start_position=0, end_position=1)
        edge.reload()

        # filter by organization
        data = {'work': edge.organization.pk}

        qs = ProjectFilterSet(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_proj)
    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)
Exemple #16
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])
Exemple #17
0
    def test_junction_point(self):
        p1 = PathFactory.create(geom=LineString((0, 0), (2, 2)))
        p2 = PathFactory.create(geom=LineString((0, 0), (2, 0)))
        p3 = PathFactory.create(geom=LineString((0, 2), (0, 0)))

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

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

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

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

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

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

        self.assertCountEqual(t.paths.all(), [p1, p2, p3])
Exemple #18
0
    def test_helpers(self):
        p = PathFactory.create()

        self.assertEquals(len(p.infrastructures), 0)
        sign = SignageFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=sign,
                                      path=p,
                                      start_position=0.5,
                                      end_position=0.5)

        self.assertItemsEqual(p.signages, [sign])

        infra = InfrastructureFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=infra, path=p)

        self.assertItemsEqual(p.infrastructures, [infra])
Exemple #19
0
    def test_remove_poi(self):
        """
        poi is linked with AB

            poi
             +                D
             *                |
             *                |
        A---------B           C
             |----|
               e1

        we got after remove AB :

             poi
              + * * * * * * * D
                              |
                              |
                              C

        poi is linked with DC and e1 is deleted
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0), (1, 0)))
        PathFactory.create(name="CD", geom=LineString((2, 0), (2, 1)))
        poi = POIFactory.create(no_path=True, offset=1)
        e1 = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(path=ab,
                                      topo_object=e1,
                                      start_position=0.5,
                                      end_position=1)
        poi.add_path(ab, start=0.5, end=0.5)
        poi.save()

        self.assertTrue(almostequal(1, poi.offset))

        self.assertEqual(poi.geom, Point(0.5, 1.0, srid=2154))

        ab.delete()
        poi.reload()
        e1.reload()

        self.assertEqual(len(Path.objects.all()), 1)

        self.assertEqual(e1.deleted, True)
        self.assertEqual(poi.deleted, False)

        self.assertTrue(almostequal(1.5, poi.offset))
    def test_filter_by_signage_management_edge(self):
        seek_proj, seek_path = self.create_pair_of_distinct_by_topo_project()

        edge = SignageManagementEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge,
                                      path=seek_path,
                                      start_position=0,
                                      end_position=1)
        edge.reload()

        # filter by organization
        data = {'signage': edge.organization.pk}

        qs = ProjectFilterSet(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_proj)
Exemple #21
0
    def test_filter_by_competence_edge(self):
        seek_proj, seek_path = self.create_pair_of_distinct_by_topo_project()

        edge = CompetenceEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge,
                                      path=seek_path,
                                      start_position=0,
                                      end_position=1)
        edge.reload()

        # filter by organization
        data = {'competence': edge.organization.pk}

        qs = ProjectFilter(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_proj)
    def test_filter_by_physical_edge(self):
        seek_inter, seek_path = self.create_pair_of_distinct_by_topo_intervention(
        )

        edge = PhysicalEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge,
                                      path=seek_path,
                                      start_position=0,
                                      end_position=1)
        edge.reload()

        # filter by physical type
        data = {'physical_type': edge.physical_type.pk}

        qs = InterventionFilterSet(data=data).qs
        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_inter)
    def test_filter_by_work_management_edge(self):
        seek_inter, seek_path = self.create_pair_of_distinct_by_topo_intervention(
        )

        edge = WorkManagementEdgeFactory(no_path=True)
        PathAggregationFactory.create(topo_object=edge,
                                      path=seek_path,
                                      start_position=0,
                                      end_position=1)
        edge.reload()

        # filter by organization
        data = {'work': edge.organization.pk}

        qs = InterventionFilterSet(data=data).qs

        self.assertEqual(len(qs), 1)
        self.assertEqual(qs[0], seek_inter)
    def create_pair_of_distinct_by_topo_intervention(self):
        p1, seek_path = self.create_pair_of_distinct_path()

        topo_1 = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=topo_1,
                                      path=p1,
                                      start_position=0,
                                      end_position=1)

        seek_topo = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=seek_topo,
                                      path=seek_path,
                                      start_position=0,
                                      end_position=1)

        InterventionFactory.create(topology=topo_1)
        seek_it = InterventionFactory.create(topology=seek_topo)
        return seek_it, seek_path
Exemple #25
0
    def test_remove_poi(self):
        """
        poi is linked with AB

            poi
             +                D
             *                |
             *                |
        A---------B           C
             |----|
               e1

        we got after remove AB :

             poi
              + * * * * * * * D
                              |
                              |
                              C

        poi is linked with DC and e1 is deleted
        """
        ab = PathFactory.create(name="AB", geom=LineString((0, 0), (1, 0)))
        PathFactory.create(name="CD", geom=LineString((2, 0), (2, 1)))
        poi = POIFactory.create(no_path=True, offset=1)
        e1 = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(path=ab, topo_object=e1, start_position=0.5, end_position=1)
        poi.add_path(ab, start=0.5, end=0.5)
        poi.save()

        self.assertTrue(almostequal(1, poi.offset))

        self.assertEqual(poi.geom, Point(0.5, 1.0, srid=2154))

        ab.delete()
        poi.reload()
        e1.reload()

        self.assertEqual(len(Path.objects.all()), 1)

        self.assertEqual(e1.deleted, True)
        self.assertEqual(poi.deleted, False)

        self.assertTrue(almostequal(1.5, poi.offset))
Exemple #26
0
    def create_pair_of_distinct_by_topo_project(self):
        p1, seek_path = self.create_pair_of_distinct_path()

        topo_1 = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=topo_1, path=p1, start_position=0, end_position=1)

        seek_topo = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=seek_topo, path=seek_path, start_position=0, end_position=1)

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

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

        proj_1 = ProjectFactory.create()
        proj_1.interventions.add(it_p1)

        return seek_proj, seek_path
Exemple #27
0
    def test_helpers(self):
        i1 = InterventionFactory.create()
        i2 = InterventionFactory.create()
        i3 = InterventionFactory.create()
        sign = SignageFactory.create()
        i1.set_topology(sign)
        p1 = sign.paths.get()

        infra = InfrastructureFactory.create()
        i2.set_topology(infra)
        p2 = infra.paths.get()

        t = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t, path=p1)
        i3.topology = t

        proj = ProjectFactory.create()
        self.assertItemsEqual(proj.paths.all(), [])
        self.assertEquals(proj.signages, [])
        self.assertEquals(proj.infrastructures, [])

        i1.save()

        proj.interventions.add(i1)
        self.assertItemsEqual(proj.paths.all(), [p1])
        self.assertEquals(proj.signages, [sign])
        self.assertEquals(proj.infrastructures, [])

        i2.save()

        proj.interventions.add(i2)
        self.assertItemsEqual(proj.paths.all(), [p1, p2])
        self.assertEquals(proj.signages, [sign])
        self.assertEquals(proj.infrastructures, [infra])

        i3.save()

        proj.interventions.add(i3)
        self.assertItemsEqual(proj.paths.all(), [p1, p2])
        self.assertEquals(proj.signages, [sign])
        self.assertEquals(proj.infrastructures, [infra])
    def test_helpers(self):
        i1 = InterventionFactory.create()
        i2 = InterventionFactory.create()
        i3 = InterventionFactory.create()
        sign = SignageFactory.create()
        i1.set_topology(sign)
        p1 = sign.paths.get()

        infra = InfrastructureFactory.create()
        i2.set_topology(infra)
        p2 = infra.paths.get()

        t = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t, path=p1)
        i3.topology = t

        proj = ProjectFactory.create()
        self.assertCountEqual(proj.paths.all(), [])
        self.assertEqual(proj.signages, [])
        self.assertEqual(proj.infrastructures, [])

        i1.save()

        proj.interventions.add(i1)
        self.assertCountEqual(proj.paths.all(), [p1])
        self.assertEqual(proj.signages, [sign])
        self.assertEqual(proj.infrastructures, [])

        i2.save()

        proj.interventions.add(i2)
        self.assertCountEqual(proj.paths.all(), [p1, p2])
        self.assertEqual(proj.signages, [sign])
        self.assertEqual(proj.infrastructures, [infra])

        i3.save()

        proj.interventions.add(i3)
        self.assertCountEqual(proj.paths.all(), [p1, p2])
        self.assertEqual(proj.signages, [sign])
        self.assertEqual(proj.infrastructures, [infra])
Exemple #29
0
 def test_poi_geojson_translation(self):
     # Create a Trek with a POI
     trek = TrekFactory.create(no_path=True)
     p1 = PathFactory.create(geom=LineString((0, 0, 0), (4, 4, 2)))
     poi = POIFactory.create(no_path=True)
     poi.name_fr = "Chapelle"
     poi.name_it = "Capela"
     poi.save()
     PathAggregationFactory.create(topo_object=trek, path=p1,
                                   start_position=0.5)
     PathAggregationFactory.create(topo_object=poi, path=p1,
                                   start_position=0.6, end_position=0.6)
     # Check that it applies to GeoJSON also :
     self.assertEqual(len(trek.pois), 1)
     poi = trek.pois[0]
     for lang, expected in [('fr-FR', poi.name_fr), ('it-IT', poi.name_it)]:
         url = reverse('trekking:trek_poi_geojson', kwargs={'pk': trek.pk})
         response = self.client.get(url, HTTP_ACCEPT_LANGUAGE=lang)
         obj = json.loads(response.content)
         jsonpoi = obj.get('features', [])[0]
         self.assertEqual(jsonpoi.get('properties', {}).get('name'), expected)
    def test_path_aggregation(self):
        """
               A---------------B + C-------------------D         A-----------------BC----------------D
                       |---------------------|                            |------------------|
                       E1                                                 E1

        2 path aggregations
        """
        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(no_path=True)
        PathAggregationFactory.create(path=path_AB, topo_object=e1, start_position=0.5, end_position=1, order=0)
        PathAggregationFactory.create(path=path_CD, topo_object=e1, start_position=0, end_position=0.5, order=1)
        path_AB.merge_path(path_CD)
        self.assertEqual(path_AB.geom, LineString((0, 1), (10, 1), (20, 1), srid=settings.SRID))
        self.assertEqual(PathAggregation.objects.filter(topo_object=e1).count(), 2)
        self.assertEqual(PathAggregation.objects.count(), 2)
        first = PathAggregation.objects.first()
        last = PathAggregation.objects.last()
        self.assertEqual((first.start_position, first.end_position), (0.25, 0.5))
        self.assertEqual((last.start_position, last.end_position), (0.5, 0.75))
        self.assertEqual(Topology.objects.count(), 1)
    def test_topology_geom(self):
        p1 = PathFactory.create(geom=LineString((0, 0), (2, 2)))
        p2 = PathFactory.create(geom=LineString((2, 2), (2, 0)))
        p3 = PathFactory.create(geom=LineString((2, 0), (4, 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), srid=settings.SRID))

        # 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), (2, 2), (2, 0), srid=settings.SRID))

        # 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), (3, 1), (4, 1), srid=settings.SRID))

        # Change offset, geometry is computed again
        t.offset = 0.5
        t.save()
        self.assertEqual(
            t.geom,
            LineString((2.5, 2), (2.5, 0.5), (4, 0.5), srid=settings.SRID))
Exemple #32
0
    def test_shape_mixed(self):
        """
        Test that a project made of intervention of different geom create multiple files.
        Check that those files are each of a different type (Point/LineString) and that
        the project and the intervention are correctly referenced in it.
        """

        # Create topology line
        topo_line = TopologyFactory.create(no_path=True)
        line = PathFactory.create(
            geom=LineString(Point(10, 10), Point(11, 10)))
        PathAggregationFactory.create(topo_object=topo_line, path=line)

        # Create a topology point
        lng, lat = tuple(
            Point(1, 1, srid=settings.SRID).transform(settings.API_SRID,
                                                      clone=True))

        closest_path = PathFactory(
            geom=LineString(Point(0, 0), Point(1, 0), srid=settings.SRID))
        topo_point = TopologyHelper._topologypoint(lng, lat, None).reload()

        self.assertEquals(topo_point.paths.get(), closest_path)

        # Create one intervention by geometry (point/linestring)
        it_point = InterventionFactory.create(topology=topo_point)
        it_line = InterventionFactory.create(topology=topo_line)
        # reload
        it_point = type(it_point).objects.get(pk=it_point.pk)
        it_line = type(it_line).objects.get(pk=it_line.pk)

        proj = ProjectFactory.create()
        proj.interventions.add(it_point)
        proj.interventions.add(it_line)

        # instanciate the class based view 'abnormally' to use create_shape directly
        # to avoid making http request, authent and reading from a zip
        pfl = ZipShapeSerializer()
        devnull = open(os.devnull, "wb")
        pfl.serialize(Project.objects.all(),
                      stream=devnull,
                      delete=False,
                      fields=ProjectFormatList.columns)
        self.assertEquals(len(pfl.layers), 2)

        ds_point = gdal.DataSource(pfl.layers.values()[0])
        layer_point = ds_point[0]
        ds_line = gdal.DataSource(pfl.layers.values()[1])
        layer_line = ds_line[0]

        self.assertEquals(layer_point.geom_type.name, 'MultiPoint')
        self.assertEquals(layer_line.geom_type.name, 'LineString')

        for layer in [layer_point, layer_line]:
            self.assertEquals(layer.srs.name, 'RGF93_Lambert_93')
            self.assertItemsEqual(layer.fields, [
                u'id', u'name', u'period', u'type', u'domain', u'constraint',
                u'global_cos', u'interventi', u'interven_1', u'comments',
                u'contractor', u'project_ow', u'project_ma', u'founders',
                u'related_st', u'insertion_', u'update_dat', u'cities',
                u'districts', u'restricted'
            ])

        self.assertEquals(len(layer_point), 1)
        self.assertEquals(len(layer_line), 1)

        for feature in layer_point:
            self.assertEquals(str(feature['id']), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_point.geom))

        for feature in layer_line:
            self.assertEquals(str(feature['id']), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_line.geom))

        # Clean-up temporary shapefiles
        for layer_file in pfl.layers.values():
            for subfile in shapefile_files(layer_file):
                os.remove(subfile)
Exemple #33
0
    def test_recompute_pk_reverse_AB_CD(self):
        """
        A---------------B + C-------------------D         B----------------AD----------------C
          |        |          |--|           |         =>    |          |     |         |--|
          E1 (0.2) |          E3 (0.2, 0.3)  |               |       E1 (0.4) |         E3 (0.8, 0.9)
                   E2 (0.6)                  E4 (0.8)       E2 (0.2)         E4 (0.6)

        In case of AB == CD, matching A and D
        """
        path_AB = PathFactory.create(name="PATH_AB",
                                     geom=LineString((10, 1), (0, 1)))
        path_CD = PathFactory.create(name="PATH_CD",
                                     geom=LineString((20, 1), (10, 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)))

        # 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, (1 - a1.start_position) *
                         (path_AB_original_length / path_AB.length))
        self.assertEqual(a1_updated.end_position, (1 - a1.end_position) *
                         (path_AB_original_length / path_AB.length))

        self.assertEqual(a2_updated.start_position, (1 - a2.start_position) *
                         (path_AB_original_length / path_AB.length))
        self.assertEqual(a2_updated.end_position, (1 - 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, (1 - a3.start_position) *
                         (path_CD_original_length / path_AB.length) +
                         path_AB_original_length / path_AB.length)
        self.assertEqual(a3_updated.end_position, (1 - a3.end_position) *
                         (path_CD_original_length / path_AB.length) +
                         path_AB_original_length / path_AB.length)

        self.assertEqual(a4_updated.start_position, (1 - a4.start_position) *
                         (path_CD_original_length / path_AB.length) +
                         path_AB_original_length / path_AB.length)
        self.assertEqual(a4_updated.end_position, (1 - a4.end_position) *
                         (path_CD_original_length / path_AB.length) +
                         path_AB_original_length / path_AB.length)

        # test offset changes
        e1_updated = Topology.objects.get(pk=e1.pk)
        self.assertEqual(e1_updated.offset, -e1.offset)
        e4_updated = Topology.objects.get(pk=e4.pk)
        self.assertEqual(e4_updated.offset, -e4.offset)
Exemple #34
0
 def create_pair_of_distinct_topologies(self, topologyfactoryclass, useless_path, seek_path):
     topo_1 = topologyfactoryclass(no_path=True)
     PathAggregationFactory.create(topo_object=topo_1, path=useless_path, start_position=0, end_position=1)
     seek_topo = topologyfactoryclass(no_path=True)
     PathAggregationFactory.create(topo_object=seek_topo, path=seek_path, start_position=0, end_position=1)
     return topo_1, seek_topo
Exemple #35
0
    def test_topology_geom_with_intermediate_markers(self):
        # Intermediate (forced passage) markers for topologies
        # Use a bifurcation, make sure computed geometry is correct
        #       +--p2---+
        #   +---+-------+---+
        #     p1   p3     p4
        p1 = PathFactory.create(geom=LineString((0, 0, 0), (2, 0, 0)))
        p2 = PathFactory.create(geom=LineString((2, 0, 0), (2, 1, 0), (4, 1, 0), (4, 0, 0)))
        p3 = PathFactory.create(geom=LineString((2, 0, 0), (4, 0, 0)))
        p4 = PathFactory.create(geom=LineString((4, 0, 0), (6, 0, 0)))
        """
        From p1 to p4, with point in the middle of p3
        """
        t = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t, path=p1)
        PathAggregationFactory.create(topo_object=t, path=p3)
        PathAggregationFactory.create(topo_object=t, path=p3,
                                      start_position=0.5, end_position=0.5)
        PathAggregationFactory.create(topo_object=t, path=p4)
        t.save()
        self.assertEqual(t.geom, LineString((0, 0, 0), (2, 0, 0), (4, 0, 0), (6, 0, 0)))
        """
        From p1 to p4, through p2
        """
        t = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t, path=p1)
        PathAggregationFactory.create(topo_object=t, path=p2)
        # There will a forced passage in database...
        PathAggregationFactory.create(topo_object=t, path=p2,
                                      start_position=0.5, end_position=0.5)
        PathAggregationFactory.create(topo_object=t, path=p4)
        t.save()
        self.assertEqual(t.geom, LineString((0, 0, 0), (2, 0, 0), (2, 1, 0), (4, 1, 0), (4, 0, 0), (6, 0, 0)))

        """
        From p1 to p4, though p2, but **with start/end at 0.0**
        """
        t2 = TopologyFactory.create(no_path=True)
        PathAggregationFactory.create(topo_object=t2, path=p1)
        PathAggregationFactory.create(topo_object=t2, path=p2)
        PathAggregationFactory.create(topo_object=t2, path=p2,
                                      start_position=0.0, end_position=0.0)
        PathAggregationFactory.create(topo_object=t2, path=p4)
        t2.save()
        self.assertEqual(t2.geom, t.geom)
Exemple #36
0
    def test_shape_mixed(self):
        """
        Test that a project made of intervention of different geom create multiple files.
        Check that those files are each of a different type (Point/LineString) and that
        the project and the intervention are correctly referenced in it.
        """

        #  Create topology line
        topo_line = TopologyFactory.create(no_path=True)
        line = PathFactory.create(geom=LineString(Point(10, 10, 0), Point(11, 10, 0)))
        PathAggregationFactory.create(topo_object=topo_line, path=line)

        #  Create a topology point
        lng, lat = tuple(Point(1, 1, srid=settings.SRID).transform(settings.API_SRID, clone=True))

        closest_path = PathFactory(geom=LineString(Point(0, 0, 0), Point(1, 0, 0), srid=settings.SRID))
        topo_point = TopologyHelper._topologypoint(lng, lat, None).reload()

        self.assertEquals(topo_point.paths.get(), closest_path)

        #  Create one intervention by geometry (point/linestring)
        it_point = InterventionFactory.create(topology=topo_point)
        it_line = InterventionFactory.create(topology=topo_line)
        #  reload
        it_point = type(it_point).objects.get(pk=it_point.pk)
        it_line = type(it_line).objects.get(pk=it_line.pk)

        proj = ProjectFactory.create()
        proj.interventions.add(it_point)
        proj.interventions.add(it_line)

        shp_creator = shape_exporter.ShapeCreator()

        #  instanciate the class based view 'abnormally' to use create_shape directly
        # to avoid making http request, authent and reading from a zip
        pfl = ZipShapeSerializer()
        pfl.create_shape(shp_creator, Project.objects.all(), ProjectFormatList.columns)

        self.assertEquals(len(shp_creator.shapes), 2)

        ds_point = gdal.DataSource(shp_creator.shapes[0][1])
        layer_point = ds_point[0]
        ds_line = gdal.DataSource(shp_creator.shapes[1][1])
        layer_line = ds_line[0]

        self.assertEquals(layer_point.geom_type.name, "MultiPoint")
        self.assertEquals(layer_line.geom_type.name, "LineString")

        for layer in [layer_point, layer_line]:
            self.assertEquals(layer.srs.name, "RGF93_Lambert_93")
            self.assertItemsEqual(layer.fields, ["domain", "name", "type", "period", "id"])

        self.assertEquals(len(layer_point), 1)
        self.assertEquals(len(layer_line), 1)

        for feature in layer_point:
            self.assertEquals(str(feature["id"]), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_point.geom))

        for feature in layer_line:
            self.assertEquals(str(feature["id"]), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_line.geom))
Exemple #37
0
 def create_pair_of_distinct_topologies(self, topologyfactoryclass, useless_path, seek_path):
     topo_1 = topologyfactoryclass(no_path=True)
     PathAggregationFactory.create(topo_object=topo_1, path=useless_path, start_position=0, end_position=1)
     seek_topo = topologyfactoryclass(no_path=True)
     PathAggregationFactory.create(topo_object=seek_topo, path=seek_path, start_position=0, end_position=1)
     return topo_1, seek_topo
 def test_topology_geom_with_intermediate_markers(self):
     # Intermediate (forced passage) markers for topologies
     # Use a bifurcation, make sure computed geometry is correct
     #       +--p2---+
     #   +---+-------+---+
     #     p1   p3     p4
     p1 = PathFactory.create(geom=LineString((0, 0), (2, 0)))
     p2 = PathFactory.create(
         geom=LineString((2, 0), (2, 1), (4, 1), (4, 0)))
     p3 = PathFactory.create(geom=LineString((2, 0), (4, 0)))
     p4 = PathFactory.create(geom=LineString((4, 0), (6, 0)))
     """
     From p1 to p4, with point in the middle of p3
     """
     t = TopologyFactory.create(no_path=True)
     PathAggregationFactory.create(topo_object=t, path=p1)
     PathAggregationFactory.create(topo_object=t, path=p3)
     PathAggregationFactory.create(topo_object=t,
                                   path=p3,
                                   start_position=0.5,
                                   end_position=0.5)
     PathAggregationFactory.create(topo_object=t, path=p4)
     t.save()
     self.assertEqual(
         t.geom,
         LineString((0, 0), (2, 0), (4, 0), (6, 0), srid=settings.SRID))
     """
     From p1 to p4, through p2
     """
     t = TopologyFactory.create(no_path=True)
     PathAggregationFactory.create(topo_object=t, path=p1)
     PathAggregationFactory.create(topo_object=t, path=p2)
     # There will a forced passage in database...
     PathAggregationFactory.create(topo_object=t,
                                   path=p2,
                                   start_position=0.5,
                                   end_position=0.5)
     PathAggregationFactory.create(topo_object=t, path=p4)
     t.save()
     self.assertEqual(
         t.geom,
         LineString((0, 0), (2, 0), (2, 1), (4, 1), (4, 0), (6, 0),
                    srid=settings.SRID))
     """
     From p1 to p4, though p2, but **with start/end at 0.0**
     """
     t2 = TopologyFactory.create(no_path=True)
     PathAggregationFactory.create(topo_object=t2, path=p1)
     PathAggregationFactory.create(topo_object=t2, path=p2)
     PathAggregationFactory.create(topo_object=t2,
                                   path=p2,
                                   start_position=0.0,
                                   end_position=0.0)
     PathAggregationFactory.create(topo_object=t2, path=p4)
     t2.save()
     self.assertEqual(t2.geom, t.geom)
Exemple #39
0
    def test_shape_mixed(self):
        """
        Test that a project made of intervention of different geom create multiple files.
        Check that those files are each of a different type (Point/LineString) and that
        the project and the intervention are correctly referenced in it.
        """

        # Create topology line
        topo_line = TopologyFactory.create(no_path=True)
        line = PathFactory.create(geom=LineString(Point(10, 10, 0), Point(11, 10, 0)))
        PathAggregationFactory.create(topo_object=topo_line, path=line)

        # Create a topology point
        lng, lat = tuple(Point(1, 1, srid=settings.SRID).transform(settings.API_SRID, clone=True))

        closest_path = PathFactory(geom=LineString(Point(0, 0, 0), Point(1, 0, 0), srid=settings.SRID))
        topo_point = TopologyHelper._topologypoint(lng, lat, None).reload()

        self.assertEquals(topo_point.paths.get(), closest_path)

        # Create one intervention by geometry (point/linestring)
        it_point = InterventionFactory.create(topology=topo_point)
        it_line = InterventionFactory.create(topology=topo_line)
        # reload
        it_point = type(it_point).objects.get(pk=it_point.pk)
        it_line = type(it_line).objects.get(pk=it_line.pk)

        proj = ProjectFactory.create()
        proj.interventions.add(it_point)
        proj.interventions.add(it_line)

        # instanciate the class based view 'abnormally' to use create_shape directly
        # to avoid making http request, authent and reading from a zip
        pfl = ZipShapeSerializer()
        devnull = open(os.devnull, "wb")
        pfl.serialize(Project.objects.all(), stream=devnull, delete=False,
                      fields=ProjectFormatList.columns)
        self.assertEquals(len(pfl.layers), 2)

        ds_point = gdal.DataSource(pfl.layers.values()[0])
        layer_point = ds_point[0]
        ds_line = gdal.DataSource(pfl.layers.values()[1])
        layer_line = ds_line[0]

        self.assertEquals(layer_point.geom_type.name, 'MultiPoint')
        self.assertEquals(layer_line.geom_type.name, 'LineString')

        for layer in [layer_point, layer_line]:
            self.assertEquals(layer.srs.name, 'RGF93_Lambert_93')
            self.assertItemsEqual(layer.fields, ['domain', 'name', 'type', 'period', 'id'])

        self.assertEquals(len(layer_point), 1)
        self.assertEquals(len(layer_line), 1)

        for feature in layer_point:
            self.assertEquals(str(feature['id']), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_point.geom))

        for feature in layer_line:
            self.assertEquals(str(feature['id']), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_line.geom))

        # Clean-up temporary shapefiles
        for layer_file in pfl.layers.values():
            for subfile in shapefile_files(layer_file):
                os.remove(subfile)