Пример #1
0
    def create_signage(self, geometry, name, infra, condition, structure,
                       description, year):
        infra_type, created = InfrastructureType.objects.get_or_create(
            label=infra, type='S')

        if created:
            self.stdout.write(
                u"- InfrastructureType '{}' created".format(infra_type))

        condition_type, created = InfrastructureCondition.objects.get_or_create(
            label=condition)

        if created:
            self.stdout.write(
                u"- Condition Type '{}' created".format(condition_type))

        structure, created = Structure.objects.get_or_create(name=structure)

        if created:
            self.stdout.write(u"- Structure '{}' created".format(structure))

        infra = Signage.objects.create(type=infra_type,
                                       name=name,
                                       condition=condition_type,
                                       structure=structure,
                                       description=description,
                                       implantation_year=year)
        serialized = '{"lng": %s, "lat": %s}' % (geometry.x, geometry.y)
        topology = TopologyHelper.deserialize(serialized)
        infra.mutate(topology)

        self.counter += 1

        return infra
Пример #2
0
 def filter_queryset(self, request, queryset, view):
     qs = queryset
     type = request.GET.get('type', None)
     if type is not None:
         qs = qs.filter(type=type)
     trek = request.GET.get('trek', None)
     if trek is not None:
         qs = TopologyHelper.overlapping(qs, Trek.objects.get(pk=trek))
     return qs
Пример #3
0
    def create_infrastructure(self, geometry, name, type, category,
                              use_structure, condition, structure, description,
                              year, verbosity, eid):

        infra_type, created = InfrastructureType.objects.get_or_create(
            label=type,
            type=category,
            structure=structure if use_structure else None)
        if created and verbosity:
            self.stdout.write(
                "- InfrastructureType '{}' created".format(infra_type))

        if condition:
            condition_type, created = InfrastructureCondition.objects.get_or_create(
                label=condition,
                structure=structure if use_structure else None)
            if created and verbosity:
                self.stdout.write(
                    "- Condition Type '{}' created".format(condition_type))
        else:
            condition_type = None

        with transaction.atomic():
            fields_without_eid = {
                'type': infra_type,
                'name': name,
                'condition': condition_type,
                'structure': structure,
                'description': description,
                'implantation_year': year
            }
            if eid:
                infra, created = Infrastructure.objects.update_or_create(
                    eid=eid, defaults=fields_without_eid)
                if verbosity > 0 and not created:
                    self.stdout.write("Update : %s with eid %s" % (name, eid))
            else:
                infra = Infrastructure.objects.create(**fields_without_eid)
        if settings.TREKKING_TOPOLOGY_ENABLED:
            try:
                geometry.coord_dim = 2
                geometry = geometry.transform(settings.API_SRID, clone=True)
                serialized = '{"lng": %s, "lat": %s}' % (geometry.x,
                                                         geometry.y)
                topology = TopologyHelper.deserialize(serialized)
                infra.mutate(topology)
            except IndexError:
                raise GEOSException('Invalid Geometry type. You need 1 path')
        else:
            if geometry.geom_type != 'Point':
                raise GEOSException('Invalid Geometry type.')
            geometry = geometry.transform(settings.SRID, clone=True)
            infra.geom = Point(geometry.x, geometry.y)
            infra.save()
        self.counter += 1

        return infra
Пример #4
0
 def create_poi(self, geometry, name, poitype):
     poitype, created = POIType.objects.get_or_create(label=poitype)
     poi = POI.objects.create(name=name, type=poitype)
     # Use existing topology helpers to transform a Point(x, y)
     # to a path aggregation (topology)
     serialized = '{"lng": %s, "lat": %s}' % (geometry.x, geometry.y)
     topology = TopologyHelper.deserialize(serialized)
     # Move deserialization aggregations to the POI
     poi.mutate(topology)
     return poi
Пример #5
0
 def create_poi(self, geometry, name, poitype):
     poitype, created = POIType.objects.get_or_create(label=poitype)
     poi = POI.objects.create(name=name, type=poitype)
     if settings.TREKKING_TOPOLOGY_ENABLED:
         # Use existing topology helpers to transform a Point(x, y)
         # to a path aggregation (topology)
         serialized = '{"lng": %s, "lat": %s}' % (geometry.x, geometry.y)
         topology = TopologyHelper.deserialize(serialized)
         # Move deserialization aggregations to the POI
         poi.mutate(topology)
     else:
         if geometry.geom_type != 'Point':
             raise TypeError
         poi.geom = Point(geometry.x, geometry.y, srid=settings.SRID)
         poi.save()
     return poi
Пример #6
0
    def create_infrastructure(self, geometry, name, type, condition, structure,
                              description, year, model, verbosity, eid):

        infra_type, created = InfrastructureType.objects.get_or_create(
            label=type, type=model, structure=None)

        if created and verbosity:
            self.stdout.write(
                u"- InfrastructureType '{}' created".format(infra_type))

        if condition:
            condition_type, created = InfrastructureCondition.objects.get_or_create(
                label=condition, structure=None)
            if created and verbosity:
                self.stdout.write(
                    u"- Condition Type '{}' created".format(condition_type))
        else:
            condition_type = None

        with transaction.atomic():
            Model = Signage if model == 'S' else Infrastructure
            fields_without_eid = {
                'type': infra_type,
                'name': name,
                'condition': condition_type,
                'structure': structure,
                'description': description,
                'implantation_year': year
            }
            if eid:
                infra, created = Model.objects.update_or_create(
                    eid=eid, defaults=fields_without_eid)
                if verbosity > 0 and not created:
                    self.stdout.write(u"Update : %s with eid %s" % (name, eid))
            else:
                infra = Model.objects.create(**fields_without_eid)

        serialized = '{"lng": %s, "lat": %s}' % (geometry.x, geometry.y)
        topology = TopologyHelper.deserialize(serialized)
        infra.mutate(topology)

        self.counter += 1

        return infra
Пример #7
0
    def create_infrastructure(self, geometry, name, type,
                              condition, structure, description, year, verbosity, eid):

        infra_type, created = SignageType.objects.get_or_create(label=type, structure=None)
        if created and verbosity:
            self.stdout.write(u"- SignageType '{}' created".format(infra_type))

        if condition:
            condition_type, created = InfrastructureCondition.objects.get_or_create(label=condition,
                                                                                    structure=None)
            if created and verbosity:
                self.stdout.write(u"- Condition Type '{}' created".format(condition_type))
        else:
            condition_type = None

        with transaction.atomic():
            fields_without_eid = {
                'type': infra_type,
                'name': name,
                'condition': condition_type,
                'structure': structure,
                'description': description,
                'implantation_year': year
            }
            if eid:
                infra, created = Signage.objects.update_or_create(
                    eid=eid,
                    defaults=fields_without_eid
                )
                if verbosity > 0 and not created:
                    self.stdout.write(u"Update : %s with eid %s" % (name, eid))
            else:
                infra = Signage.objects.create(**fields_without_eid)

        serialized = '{"lng": %s, "lat": %s}' % (geometry.x, geometry.y)
        topology = TopologyHelper.deserialize(serialized)
        infra.mutate(topology)

        self.counter += 1

        return infra
    def create_infrastructure(self, geometry, name, type, condition, structure,
                              description, year, model, verbosity):

        infra_type, created = InfrastructureType.objects.get_or_create(
            label=type, type=model)

        if created and verbosity:
            self.stdout.write(
                u"- InfrastructureType '{}' created".format(infra_type))

        condition_type, created = InfrastructureCondition.objects.get_or_create(
            label=condition)

        if created and verbosity:
            self.stdout.write(
                u"- Condition Type '{}' created".format(condition_type))

        structure, created = Structure.objects.get_or_create(name=structure)

        if created and verbosity:
            self.stdout.write(u"- Structure '{}' created".format(structure))
        with transaction.atomic():
            Model = Signage if model == 'S' else Infrastructure
            infra = Model.objects.create(type=infra_type,
                                         name=name,
                                         condition=condition_type,
                                         structure=structure,
                                         description=description,
                                         implantation_year=year)
        serialized = '{"lng": %s, "lat": %s}' % (geometry.x, geometry.y)
        topology = TopologyHelper.deserialize(serialized)
        infra.mutate(topology)

        self.counter += 1

        return infra
Пример #9
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)
Пример #10
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)
Пример #11
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

        line = PathFactory.create(
            geom=LineString(Point(10, 10), Point(11, 10)))
        topo_line = TopologyFactory.create(paths=[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.assertEqual(topo_point.paths.get(), closest_path)

        # Create one intervention by geometry (point/linestring)
        it_point = InterventionFactory.create(target=topo_point)
        it_line = InterventionFactory.create(target=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()
        shapefiles = pfl.path_directory
        devnull = open(os.devnull, "wb")
        pfl.serialize(Project.objects.all(),
                      stream=devnull,
                      delete=False,
                      fields=ProjectFormatList.columns)
        shapefiles = [
            shapefile for shapefile in os.listdir(shapefiles)
            if shapefile[-3:] == "shp"
        ]
        datasources = [
            gdal.DataSource(os.path.join(pfl.path_directory, s))
            for s in shapefiles
        ]
        layers = [ds[0] for ds in datasources]

        self.assertEqual(len(datasources), 2)
        geom_type_layer = {layer.name: layer for layer in layers}
        geom_types = geom_type_layer.keys()
        self.assertIn('MultiPoint', geom_types)
        self.assertIn('MultiLineString', geom_types)

        for layer in layers:
            self.assertEqual(layer.srs.name, 'RGF93_Lambert_93')
            self.assertCountEqual(layer.fields, [
                'id', 'name', 'period', 'type', 'domain', 'constraint',
                'global_cos', 'interventi', 'comments', 'contractor',
                'project_ow', 'project_ma', 'founders', 'related_st',
                'insertion_', 'update_dat', 'cities', 'districts', 'restricted'
            ])

        self.assertEqual(len(layers[0]), 1)
        self.assertEqual(len(layers[1]), 1)

        for feature in geom_type_layer['MultiPoint']:
            self.assertEqual(str(feature['id']), str(proj.pk))
            self.assertEqual(len(feature.geom.geos), 1)
            self.assertAlmostEqual(feature.geom.geos[0].x, it_point.geom.x)
            self.assertAlmostEqual(feature.geom.geos[0].y, it_point.geom.y)

        for feature in geom_type_layer['MultiLineString']:
            self.assertEqual(str(feature['id']), str(proj.pk))
            self.assertTrue(feature.geom.geos.equals(it_line.geom))
Пример #12
0
    def test_link_closest_visible_path(self):
        """
        Topology must be linked to the closest visible path only
        """
        path_visible = Path(name="visible",
                            geom='LINESTRING(0 0, 1 0, 2 0)',
                            visible=True)
        path_visible.save()
        path_unvisible = Path(name="unvisible",
                              geom='LINESTRING(0 3, 1 3, 2 3)',
                              visible=False)
        path_unvisible.save()

        # default manager see 1 path
        self.assertEqual(Path.objects.count(), 1)

        # custom manager see 2 paths
        self.assertEqual(Path.include_invisible.count(), 2)

        # create topo on visible path
        topology = TopologyHelper._topologypoint(0, 0, None).reload()

        # because FK and M2M are used with default manager only, others tests are in SQL
        conn = connections[DEFAULT_DB_ALIAS]
        cur = conn.cursor()
        cur.execute("""
            SELECT t.id as id_path,
                   et.evenement as id_topology,
                   t.visible as visible
            FROM e_r_evenement_troncon et
            JOIN l_t_troncon t ON et.troncon=t.id
            WHERE et.evenement={topo_id}
            """.format(topo_id=topology.pk))

        datas = dictfetchall(cur)

        # topo must be linked to visible path
        self.assertIn(topology.pk, [ele['id_topology'] for ele in datas],
                      u"{}".format(datas))
        self.assertIn(path_visible.pk, [ele['id_path'] for ele in datas],
                      u"{}".format(datas))
        self.assertNotIn(path_unvisible.pk, [ele['id_path'] for ele in datas],
                         u"{}".format(datas))

        # new topo on invible path
        topology = TopologyHelper._topologypoint(0, 3, None).reload()

        cur.execute("""
            SELECT t.id as id_path,
                   et.evenement as id_topology,
                   t.visible as visible
            FROM e_r_evenement_troncon et
            JOIN l_t_troncon t ON et.troncon=t.id
            WHERE et.evenement={topo_id}
            """.format(topo_id=topology.pk))

        datas = dictfetchall(cur)

        self.assertIn(topology.pk, [ele['id_topology'] for ele in datas],
                      u"{}".format(datas))
        self.assertIn(path_visible.pk, [ele['id_path'] for ele in datas],
                      u"{}".format(datas))
        self.assertNotIn(path_unvisible.pk, [ele['id_path'] for ele in datas],
                         u"{}".format(datas))
        cur.close()
Пример #13
0
    def test_link_closest_visible_path(self):
        """
        Topology must be linked to the closest visible path only
        """
        path_visible = Path(name="visible",
                            geom='LINESTRING(0 0, 1 0, 2 0)',
                            visible=True)
        path_visible.save()
        path_unvisible = Path(name="unvisible",
                              geom='LINESTRING(0 3, 1 3, 2 3)',
                              visible=False)
        path_unvisible.save()

        # default manager see 1 path
        self.assertEqual(Path.objects.count(), 1)

        # custom manager see 2 paths
        self.assertEqual(Path.include_invisible.count(), 2)

        # create topo on visible path
        topology = TopologyHelper._topologypoint(0, 0, None).reload()

        # because FK and M2M are used with default manager only, others tests are in SQL
        conn = connections[DEFAULT_DB_ALIAS]
        cur = conn.cursor()
        cur.execute(
            """
            SELECT t.id as id_path,
                   et.evenement as id_topology,
                   t.visible as visible
            FROM e_r_evenement_troncon et
            JOIN l_t_troncon t ON et.troncon=t.id
            WHERE et.evenement={topo_id}
            """.format(topo_id=topology.pk))

        datas = dictfetchall(cur)

        # topo must be linked to visible path
        self.assertIn(topology.pk, [ele['id_topology'] for ele in datas], u"{}".format(datas))
        self.assertIn(path_visible.pk, [ele['id_path'] for ele in datas], u"{}".format(datas))
        self.assertNotIn(path_unvisible.pk, [ele['id_path'] for ele in datas], u"{}".format(datas))

        # new topo on invible path
        topology = TopologyHelper._topologypoint(0, 3, None).reload()

        cur.execute(
            """
            SELECT t.id as id_path,
                   et.evenement as id_topology,
                   t.visible as visible
            FROM e_r_evenement_troncon et
            JOIN l_t_troncon t ON et.troncon=t.id
            WHERE et.evenement={topo_id}
            """.format(topo_id=topology.pk))

        datas = dictfetchall(cur)

        self.assertIn(topology.pk, [ele['id_topology'] for ele in datas], u"{}".format(datas))
        self.assertIn(path_visible.pk, [ele['id_path'] for ele in datas], u"{}".format(datas))
        self.assertNotIn(path_unvisible.pk, [ele['id_path'] for ele in datas], u"{}".format(datas))
        cur.close()
Пример #14
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))