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)
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)
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))
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()
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()
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))