def test_splitFeaturesBlock_Y_return_two(self): #take a list with features with simlar attributes. #Return a list of features, features from same street will merged to continous LINESTRING from osgeo import ogr, osr import mergelines processor = mergelines.Processor() # set up the shapefile driver driver = ogr.GetDriverByName("MEMORY") # create the data source data_source = driver.CreateDataSource("memData") srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = data_source.CreateLayer("memData", srs, ogr.wkbLineString) features = list() features_etalon = list() line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.1, 55.1) line.AddPoint(37.2, 55.2) line.AddPoint(37.3, 55.3) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(line) features.append(feature) line = None feature = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.3, 55.3) line.AddPoint(37.4, 55.3) line.AddPoint(37.5, 55.3) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(line) features.append(feature) line = None feature = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.3, 55.3) line.AddPoint(37.25, 55.3) line.AddPoint(37.15, 55.5) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(line) features.append(feature) line = None feature = None #------------------------- mfeatures = processor.splitFeaturesBlock(features, layer.GetLayerDefn()) self.assertEqual(len(mfeatures), 2)
def test_withClusterTouchingFeature_standart(self): from osgeo import ogr, osr, gdal import mergelines processor = mergelines.Processor() clusters_geometry = dict() #dict of geometry of linestrings line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.1, 55.1) line.AddPoint(37.2, 55.2) line.AddPoint(37.3, 55.3) line.AddPoint(37.4, 55.4) line.AddPoint(37.5, 55.5) line.AddPoint(37.6, 55.6) clusters_geometry[0] = line line = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(38.1, 55.1) line.AddPoint(38.2, 55.2) line.AddPoint(38.3, 55.3) line.AddPoint(38.4, 55.4) line.AddPoint(38.5, 55.5) line.AddPoint(38.6, 55.6) clusters_geometry[1] = line line = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(38.6, 55.6) line.AddPoint(38.7, 55.7) testing_line = line line = None self.assertEqual( processor.withClusterTouchingGeometry(clusters_geometry, testing_line), 1) line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.6, 55.6) line.AddPoint(38.7, 55.7) testing_line = line line = None self.assertEqual( processor.withClusterTouchingGeometry(clusters_geometry, testing_line), 0) line = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(38.8, 55.6) line.AddPoint(38.7, 55.7) testing_line = line line = None self.assertNotEqual( processor.withClusterTouchingGeometry(clusters_geometry, testing_line), 1)
def test_splitFeaturesBlock_onefeature_return_one(self): #take a list with features with simlar attributes. #Return a list of features, features from same street will merged to continous LINESTRING from osgeo import ogr, osr import mergelines processor = mergelines.Processor() # set up the shapefile driver driver = ogr.GetDriverByName("MEMORY") # create the data source data_source = driver.CreateDataSource("memData") srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = data_source.CreateLayer("memData", srs, ogr.wkbLineString) features = list() features_etalon = list() line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.1, 55.1) line.AddPoint(37.2, 55.2) line.AddPoint(37.3, 55.3) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(line) features.append(feature) line = None feature = None #------------------------- line = ogr.Geometry(ogr.wkbLineString) line.AddPoint(37.1, 55.1) line.AddPoint(37.2, 55.2) line.AddPoint(37.3, 55.3) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(line) features_etalon.append(feature) line = None feature = None mfeatures = processor.splitFeaturesBlock(features, layer.GetLayerDefn()) self.assertTrue(len(mfeatures) == len(features_etalon)) #тут надо проверить геометрию полученого списка но это слишком сложный тест был был for i in range(0, len(mfeatures)): g = mfeatures[i].GetGeometryRef() wkt = g.ExportToWkt() g_etalon = features_etalon[i].GetGeometryRef() wkt_etalon = g_etalon.ExportToWkt() self.assertEqual(wkt, wkt_etalon)
def test_geometryMerge_case3(self): from osgeo import ogr, osr, gdal import mergelines processor = mergelines.Processor() line = ogr.Geometry(ogr.wkbLineString) line.AddPoint_2D(37.6, 55.6) line.AddPoint_2D(37.5, 55.5) line.AddPoint_2D(37.4, 55.4) line.AddPoint_2D(37.3, 55.3) line.AddPoint_2D(37.2, 55.2) line.AddPoint_2D(37.1, 55.1) source_line = line line = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint_2D(37.6, 55.6) line.AddPoint_2D(37.75, 55.75) line.AddPoint_2D(37.85, 55.85) append_line_1 = line line = None line = ogr.Geometry(ogr.wkbLineString) line.AddPoint_2D(37.85, 55.85) line.AddPoint_2D(37.75, 55.75) line.AddPoint_2D(37.6, 55.6) line.AddPoint_2D(37.5, 55.5) line.AddPoint_2D(37.4, 55.4) line.AddPoint_2D(37.3, 55.3) line.AddPoint_2D(37.2, 55.2) line.AddPoint_2D(37.1, 55.1) etalon_line = line line = None merge_result = processor.geometry_merge(source_line, append_line_1) compare_result = processor.compareGeom(merge_result, etalon_line) self.assertTrue(compare_result) source_line = None append_line_1 = None merge_result = None etalon_line = None
def test_filterIsolateFeatures_manyfeatures_6x2_return(self): from osgeo import ogr, osr, gdal import mergelines processor = mergelines.Processor() dataSource = gdal.OpenEx('unittest/test_filter_isolate_many.geojson') layer = dataSource.GetLayer() source_features = list() for feature in layer: source_features.append(feature) non_isolated_features, isolated_features = processor.filterIsolateFeatures( source_features) self.assertEqual(len(non_isolated_features), 6) self.assertEqual(len(isolated_features), 2) self.assertNotEqual(len(isolated_features), 68) layer = None dataSource = None
def test_get_last_point(self): from osgeo import ogr, osr import mergelines driver = ogr.GetDriverByName("MEMORY") data_source = driver.CreateDataSource("memData") srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = data_source.CreateLayer("memData", srs, ogr.wkbLineString) line = ogr.Geometry(ogr.wkbLineString) #line.AddPoint(37.0, 55.0) line.AddPoint(37.1, 55.1) line.AddPoint(37.2, 55.2) line.AddPoint(37.3, 55.3) line.AddPoint(37.4, 55.4) line.AddPoint(37.5, 55.5) line.AddPoint(37.6, 55.6) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(line) processor = mergelines.Processor() last_point = processor.getLastPointOfLine(feature.GetGeometryRef()) etalon_point = ogr.Geometry(ogr.wkbPoint) etalon_point.AddPoint_2D(37.6, 55.6) compare_result = processor.compareGeom(last_point, etalon_point) self.assertTrue(compare_result) etalon_point = None feature = None etalon_point = ogr.Geometry(ogr.wkbPoint) etalon_point.AddPoint_2D(37.9, 55.9) compare_result = processor.compareGeom(last_point, etalon_point) self.assertFalse(compare_result) etalon_point = None feature = None
def create_output_layer(self): import os from osgeo import ogr, osr filename = 'mergelines.geojson' if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename)) import mergelines processor = mergelines.Processor() driver = ogr.GetDriverByName("MEMORY") # create the data source data_source = driver.CreateDataSource("memData") srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = data_source.CreateLayer("memData", srs, ogr.wkbLineString) processor.create_output_layer(layer) self.assertTrue(os.path.exists(filename)) os.remove(filename) self.assertFalse(os.path.exists(filename))
def test_compare_geom(self): from osgeo import ogr, osr import mergelines processor = mergelines.Processor() etalon_point1 = ogr.Geometry(ogr.wkbPoint) etalon_point1.AddPoint_2D(37.1, 55.1) etalon_point2 = ogr.Geometry(ogr.wkbPoint) etalon_point2.AddPoint_2D(37.1, 55.1) compare_result = processor.compareGeom(etalon_point1, etalon_point2) self.assertTrue(compare_result) etalon_point1 = None etalon_point2 = None etalon_point1 = ogr.Geometry(ogr.wkbPoint) etalon_point1.AddPoint_2D(37.2, 55.1) etalon_point2 = ogr.Geometry(ogr.wkbPoint) etalon_point2.AddPoint_2D(37.1, 55.1) compare_result = processor.compareGeom(etalon_point1, etalon_point2) self.assertFalse(compare_result) etalon_point1 = None etalon_point2 = None etalon_point1 = ogr.Geometry(ogr.wkbPoint) etalon_point1.AddPoint_2D(-37.1, 55.1) etalon_point2 = ogr.Geometry(ogr.wkbPoint) etalon_point2.AddPoint_2D(-37.1, 55.1) compare_result = processor.compareGeom(etalon_point1, etalon_point2) self.assertTrue(compare_result) etalon_point1 = None etalon_point2 = None
#!/usr/bin/env python # -*- coding: utf-8 -*- from osgeo import gdal from osgeo import ogr import os import errno import logging import mergelines processor = mergelines.Processor( '/home/trolleway/ssdgis/mergeline/testdata/hi2.gpkg') #only LINESTRING processor.mergelines( output_filename='/home/trolleway/ssdgis/mergeline/testdata/result.gpkg', DifferentFeaturesList=('NAME', 'HIGHWAY')) #only gpkg ''' ogr2ogr -nlt Linestring /home/trolleway/ssdgis/mergeline/testdata/hi2.gpkg /home/trolleway/ssdgis/mergeline/testdata/hi1.gpkg '''