def test_multilinestring(self): # From coordinate tuples geom = MultiLineString((((1.0, 2.0), (3.0, 4.0)),)) self.assertIsInstance(geom, MultiLineString) self.assertEqual(len(geom.geoms), 1) self.assertEqual(dump_coords(geom), [[(1.0, 2.0), (3.0, 4.0)]]) # From lines a = LineString(((1.0, 2.0), (3.0, 4.0))) ml = MultiLineString([a]) self.assertEqual(len(ml.geoms), 1) self.assertEqual(dump_coords(ml), [[(1.0, 2.0), (3.0, 4.0)]]) # From another multi-line ml2 = MultiLineString(ml) self.assertEqual(len(ml2.geoms), 1) self.assertEqual(dump_coords(ml2), [[(1.0, 2.0), (3.0, 4.0)]]) # Sub-geometry Access geom = MultiLineString([(((0.0, 0.0), (1.0, 2.0)))]) self.assertIsInstance(geom[0], LineString) self.assertEqual(dump_coords(geom[0]), [(0.0, 0.0), (1.0, 2.0)]) with self.assertRaises(IndexError): # index out of range geom.geoms[1] # Geo interface self.assertEqual(geom.__geo_interface__, {'type': 'MultiLineString', 'coordinates': (((0.0, 0.0), (1.0, 2.0)),)})
def test_multipoint(self): # From coordinate tuples geom = MultiLineString((((1.0, 2.0), (3.0, 4.0)), )) self.assertIsInstance(geom, MultiLineString) self.assertEqual(len(geom.geoms), 1) self.assertEqual(dump_coords(geom), [[(1.0, 2.0), (3.0, 4.0)]]) # From lines a = LineString(((1.0, 2.0), (3.0, 4.0))) ml = MultiLineString([a]) self.assertEqual(len(ml.geoms), 1) self.assertEqual(dump_coords(ml), [[(1.0, 2.0), (3.0, 4.0)]]) # From another multi-line ml2 = MultiLineString(ml) self.assertEqual(len(ml2.geoms), 1) self.assertEqual(dump_coords(ml2), [[(1.0, 2.0), (3.0, 4.0)]]) # Sub-geometry Access geom = MultiLineString([(((0.0, 0.0), (1.0, 2.0)))]) self.assertIsInstance(geom[0], LineString) self.assertEqual(dump_coords(geom[0]), [(0.0, 0.0), (1.0, 2.0)]) with self.assertRaises(IndexError): # index out of range geom.geoms[1] # Geo interface self.assertEqual(geom.__geo_interface__, { 'type': 'MultiLineString', 'coordinates': (((0.0, 0.0), (1.0, 2.0)), ) })
def test_numpy(self): from numpy import array, asarray from numpy.testing import assert_array_equal # Construct from a numpy array geom = MultiPoint(array([[0.0, 0.0], [1.0, 2.0]])) self.assertIsInstance(geom, MultiPoint) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(0.0, 0.0)], [(1.0, 2.0)]]) # Geo interface (cont.) geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) assert_array_equal(array(geom), array([[1., 2.], [3., 4.]])) # Adapt a Numpy array to a multipoint a = array([[1.0, 2.0], [3.0, 4.0]]) geoma = asMultiPoint(a) assert_array_equal(geoma.context, array([[1., 2.], [3., 4.]])) self.assertEqual(dump_coords(geoma), [[(1.0, 2.0)], [(3.0, 4.0)]]) # Now, the inverse self.assertEqual(geoma.__array_interface__, geoma.context.__array_interface__) pas = asarray(geoma) assert_array_equal(pas, array([[1., 2.], [3., 4.]]))
def test_multipoint(self): # From coordinate tuples geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(1.0, 2.0)], [(3.0, 4.0)]]) # From points geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(1.0, 2.0)], [(3.0, 4.0)]]) # From another multi-point geom2 = MultiPoint(geom) self.assertEqual(len(geom2.geoms), 2) self.assertEqual(dump_coords(geom2), [[(1.0, 2.0)], [(3.0, 4.0)]]) # Sub-geometry Access self.assertIsInstance(geom.geoms[0], Point) self.assertEqual(geom.geoms[0].x, 1.0) self.assertEqual(geom.geoms[0].y, 2.0) with self.assertRaises(IndexError): # index out of range geom.geoms[2] # Geo interface self.assertEqual(geom.__geo_interface__, {'type': 'MultiPoint', 'coordinates': ((1.0, 2.0), (3.0, 4.0))}) # Adapt a coordinate list to a line string coords = [[5.0, 6.0], [7.0, 8.0]] geoma = asMultiPoint(coords) self.assertEqual(dump_coords(geoma), [[(5.0, 6.0)], [(7.0, 8.0)]])
def test_multilinestring(self): # From coordinate tuples geom = MultiLineString((((1.0, 2.0), (3.0, 4.0)), )) assert isinstance(geom, MultiLineString) assert len(geom.geoms) == 1 assert dump_coords(geom) == [[(1.0, 2.0), (3.0, 4.0)]] # From lines a = LineString(((1.0, 2.0), (3.0, 4.0))) ml = MultiLineString([a]) assert len(ml.geoms) == 1 assert dump_coords(ml) == [[(1.0, 2.0), (3.0, 4.0)]] # From another multi-line ml2 = MultiLineString(ml) assert len(ml2.geoms) == 1 assert dump_coords(ml2) == [[(1.0, 2.0), (3.0, 4.0)]] # Sub-geometry Access geom = MultiLineString([(((0.0, 0.0), (1.0, 2.0)))]) assert isinstance(geom.geoms[0], LineString) assert dump_coords(geom.geoms[0]) == [(0.0, 0.0), (1.0, 2.0)] with pytest.raises(IndexError): # index out of range geom.geoms[1] # Geo interface assert geom.__geo_interface__ == { "type": "MultiLineString", "coordinates": (((0.0, 0.0), (1.0, 2.0)), ), }
def test_multipoint(self): # From coordinate tuples geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) assert len(geom.geoms) == 2 assert dump_coords(geom) == [[(1.0, 2.0)], [(3.0, 4.0)]] # From points geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) assert len(geom.geoms) == 2 assert dump_coords(geom) == [[(1.0, 2.0)], [(3.0, 4.0)]] # From another multi-point geom2 = MultiPoint(geom) assert len(geom2.geoms) == 2 assert dump_coords(geom2) == [[(1.0, 2.0)], [(3.0, 4.0)]] # Sub-geometry Access assert isinstance(geom.geoms[0], Point) assert geom.geoms[0].x == 1.0 assert geom.geoms[0].y == 2.0 with pytest.raises(IndexError): # index out of range geom.geoms[2] # Geo interface assert geom.__geo_interface__ == { "type": "MultiPoint", "coordinates": ((1.0, 2.0), (3.0, 4.0)), }
def test_multipoint(self): # From coordinate tuples geom = MultiPoint(((1.0, 2.0), (3.0, 4.0))) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(1.0, 2.0)], [(3.0, 4.0)]]) # From points geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(1.0, 2.0)], [(3.0, 4.0)]]) # From another multi-point geom2 = MultiPoint(geom) self.assertEqual(len(geom2.geoms), 2) self.assertEqual(dump_coords(geom2), [[(1.0, 2.0)], [(3.0, 4.0)]]) # Sub-geometry Access self.assertIsInstance(geom.geoms[0], Point) self.assertEqual(geom.geoms[0].x, 1.0) self.assertEqual(geom.geoms[0].y, 2.0) with self.assertRaises(IndexError): # index out of range geom.geoms[2] # Geo interface self.assertEqual(geom.__geo_interface__, {'type': 'MultiPoint', 'coordinates': ((1.0, 2.0), (3.0, 4.0))})
def points_kml(df, file_name, wkt_column=None, name_column=None, description_columns='all', exclude_columns=None, altitude=0, label_scale=0.8, \ color=simplekml.Color.white, alpha=255, color_mode=simplekml.ColorMode.normal, icon_href='http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png'): """ Generate KML file with Points/MultiPoints layer Parameters: ----------- df - pandas dataframe with WKT geometry; file_name - name of the KML file; wkt_column - column name of the dataframe with WKT geometry (if ommited, the last column will be taken); name_column - column name of the dataframe with names for the geometries (if ommited, the dataframe index will be taken); descrition_columns - list of column names that will be set in description balloon. If set 'all', all the columns but wkt_column and name_column will be taken; exclude_columns - list of column names that will be excluded from the description_columns; altitude - an altitude value for the geometries; label_scale - scale of the label; color - a color for the geometries (read more: https://simplekml.readthedocs.io/en/latest/constants.html?#color) alpha - level of opacity from 0 to 255; color_mode - normal/random; icon_href - href for the icons; """ file_name = _process_file_name(file_name) description_columns = _process_description_columns(df, wkt_column, name_column, description_columns, exclude_columns) kml = simplekml.Kml() sharedstyle = simplekml.Style() sharedstyle.iconstyle.icon.href = icon_href sharedstyle = _process_color('Point', sharedstyle, color_mode, color, alpha) sharedstyle.labelstyle.scale = label_scale for index, row in df.iterrows(): shape = wkt.loads(row[wkt_column]) if wkt_column else wkt.loads( row[-1]) name = str(row[name_column]) if name_column else str(index) description = _process_description(row, description_columns) logging.debug(f'shape_type: {shape.type}') if shape.type == 'Point': outer_boundary, _ = _process_boundaries(dump_coords(shape), altitude) pnt = kml.newpoint( name=name, description=description, coords=outer_boundary, altitudemode=simplekml.AltitudeMode.relativetoground) pnt.extrude = 1 pnt.style = sharedstyle elif shape.type == 'MultiPoint': multipnt = kml.newmultigeometry(name=name, description=description) for coords_list in dump_coords(shape): outer_boundary, _ = _process_boundaries(coords_list, altitude) pnt = multipnt.newpoint( coords=outer_boundary, altitudemode=simplekml.AltitudeMode.relativetoground) pnt.extrude = 1 multipnt.style = sharedstyle else: print(f'{name} has bad geometry') kml.save(file_name)
def lines_kml(df, file_name, wkt_column=None, name_column=None, description_columns='all', exclude_columns=None, altitude=0, width=3, \ color=simplekml.Color.red, alpha=200, color_mode=simplekml.ColorMode.normal, label_visibility=False): """ Generate KML file with LineStrings/MultiLineStrings layer Parameters: ----------- df - pandas dataframe with WKT geometry; file_name - name of the KML file; wkt_column - column name of the dataframe with WKT geometry (if ommited, the last column will be taken); name_column - column name of the dataframe with names for the geometries (if ommited, the dataframe index will be taken); descrition_columns - list of column names that will be set in description balloon. If set 'all', all the columns but wkt_column and name_column will be taken; exclude_columns - list of column names that will be excluded from the description_columns; altitude - an altitude value for the geometries; width - width of the lines; color - a color for the geometries (read more: https://simplekml.readthedocs.io/en/latest/constants.html?#color) alpha - level of opacity from 0 to 255; color_mode - normal/random; label_visibility - whether labels will be visible or not; """ file_name = _process_file_name(file_name) description_columns = _process_description_columns(df, wkt_column, name_column, description_columns, exclude_columns) kml = simplekml.Kml() sharedstyle = simplekml.Style() sharedstyle = _process_color('LineString', sharedstyle, color_mode, color, alpha) sharedstyle.linestyle.width = width sharedstyle.linestyle.gxlabelvisibility = label_visibility for index, row in df.iterrows(): shape = wkt.loads(row[wkt_column]) if wkt_column else wkt.loads( row[-1]) name = str(row[name_column]) if name_column else str(index) description = _process_description(row, description_columns) logging.debug(f'shape_type: {shape.type}') if shape.type == 'LineString': outer_boundary, _ = _process_boundaries(dump_coords(shape), altitude) ls = kml.newlinestring( name=name, description=description, coords=outer_boundary, altitudemode=simplekml.AltitudeMode.relativetoground) ls.extrude = 1 ls.style = sharedstyle elif shape.type == 'MultiLineString': multils = kml.newmultigeometry(name=name, description=description) for coords_list in dump_coords(shape): outer_boundary, _ = _process_boundaries(coords_list, altitude) ls = multils.newlinestring( coords=outer_boundary, altitudemode=simplekml.AltitudeMode.relativetoground) ls.extrude = 1 multils.style = sharedstyle else: print(f'{name} has bad geometry') kml.save(file_name)
def test_linearring_adapter(self): # Test ring adapter coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]] ra = asLinearRing(coords) self.assertTrue(ra.wkt.upper().startswith('LINEARRING')) self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]) coords[3] = [2.0, -1.0] self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)])
def test_multipolygon(self): # From coordinate tuples geom = MultiPolygon([(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])]) self.assertIsInstance(geom, MultiPolygon) self.assertEqual(len(geom.geoms), 1) self.assertEqual(dump_coords(geom), [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]]) # Or from polygons p = Polygon(((0, 0), (0, 1), (1, 1), (1, 0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]) geom = MultiPolygon([p]) self.assertEqual(len(geom.geoms), 1) self.assertEqual(dump_coords(geom), [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]]) # Or from another multi-polygon geom2 = MultiPolygon(geom) self.assertEqual(len(geom2.geoms), 1) self.assertEqual(dump_coords(geom2), [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]]) # Sub-geometry Access self.assertIsInstance(geom.geoms[0], Polygon) self.assertEqual(dump_coords(geom.geoms[0]), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]) with self.assertRaises(IndexError): # index out of range geom.geoms[1] # Geo interface self.assertEqual( geom.__geo_interface__, { 'type': 'MultiPolygon', 'coordinates': [(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)), ((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)))] })
def test_from_multilinestring_z(self): coords1 = [(0.0, 1.0, 2.0), (3.0, 4.0, 5.0)] coords2 = [(6.0, 7.0, 8.0), (9.0, 10.0, 11.0)] # From coordinate tuples ml = MultiLineString([coords1, coords2]) copy = MultiLineString(ml) assert isinstance(copy, MultiLineString) assert copy.geom_type == "MultiLineString" assert len(copy.geoms) == 2 assert dump_coords(copy.geoms[0]) == coords1 assert dump_coords(copy.geoms[1]) == coords2
def test_from_multilinestring_z(self): coords1 = [(0.0, 1.0, 2.0), (3.0, 4.0, 5.0)] coords2 = [(6.0, 7.0, 8.0), (9.0, 10.0, 11.0)] # From coordinate tuples ml = MultiLineString([coords1, coords2]) copy = MultiLineString(ml) self.assertIsInstance(copy, MultiLineString) self.assertEqual('MultiLineString', lgeos.GEOSGeomType(copy._geom).decode('ascii')) self.assertEqual(len(copy.geoms), 2) self.assertEqual(dump_coords(copy.geoms[0]), coords1) self.assertEqual(dump_coords(copy.geoms[1]), coords2)
def test_numpy(self): from numpy import array from numpy.testing import assert_array_equal # Construct from a numpy array geom = MultiLineString([array(((0.0, 0.0), (1.0, 2.0)))]) self.assertIsInstance(geom, MultiLineString) self.assertEqual(len(geom.geoms), 1) self.assertEqual(dump_coords(geom), [[(0.0, 0.0), (1.0, 2.0)]]) # Adapt a sequence of Numpy arrays to a multilinestring a = [array(((1.0, 2.0), (3.0, 4.0)))] geoma = asMultiLineString(a) assert_array_equal(geoma.context, [array([[1., 2.], [3., 4.]])]) self.assertEqual(dump_coords(geoma), [[(1.0, 2.0), (3.0, 4.0)]])
def test_numpy_adapter(self): from numpy import array from numpy.testing import assert_array_equal # Adapt a sequence of Numpy arrays to a multilinestring a = [array(((1.0, 2.0), (3.0, 4.0)))] geoma = asMultiLineString(a) assert_array_equal(geoma.context, [array([[1., 2.], [3., 4.]])]) self.assertEqual(dump_coords(geoma), [[(1.0, 2.0), (3.0, 4.0)]])
def test_multipoint_from_numpy(self): from numpy import array, asarray from numpy.testing import assert_array_equal # Construct from a numpy array geom = MultiPoint(array([[0.0, 0.0], [1.0, 2.0]])) self.assertIsInstance(geom, MultiPoint) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(0.0, 0.0)], [(1.0, 2.0)]])
def test_numpy(self): from numpy import array from numpy.testing import assert_array_equal # Construct from a numpy array geom = MultiLineString([array(((0.0, 0.0), (1.0, 2.0)))]) self.assertIsInstance(geom, MultiLineString) self.assertEqual(len(geom.geoms), 1) self.assertEqual(dump_coords(geom), [[(0.0, 0.0), (1.0, 2.0)]])
def test_polygon_from_numpy(self): from numpy import array, asarray from numpy.testing import assert_array_equal a = asarray(((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))) polygon = Polygon(a) self.assertEqual(len(polygon.exterior.coords), 5) self.assertEqual(dump_coords(polygon.exterior), [(0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)]) self.assertEqual(len(polygon.interiors), 0)
def test_numpy(self): from numpy import array, asarray from numpy.testing import assert_array_equal # Construct from a numpy array geom = MultiPoint(array([[0.0, 0.0], [1.0, 2.0]])) self.assertIsInstance(geom, MultiPoint) self.assertEqual(len(geom.geoms), 2) self.assertEqual(dump_coords(geom), [[(0.0, 0.0)], [(1.0, 2.0)]]) # Geo interface (cont.) geom = MultiPoint((Point(1.0, 2.0), Point(3.0, 4.0))) assert_array_equal(array(geom), array([[1., 2.], [3., 4.]]))
def test_numpy(self): from numpy import array, asarray from numpy.testing import assert_array_equal a = asarray(((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))) polygon = Polygon(a) self.assertEqual(len(polygon.exterior.coords), 5) self.assertEqual(dump_coords(polygon.exterior), [(0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)]) self.assertEqual(len(polygon.interiors), 0) b = asarray(polygon.exterior) self.assertEqual(b.shape, (5, 2)) assert_array_equal( b, array([(0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)]))
def test_numpy_adapter(self): from numpy import array, asarray from numpy.testing import assert_array_equal # Adapt a Numpy array to a multipoint a = array([[1.0, 2.0], [3.0, 4.0]]) geoma = asMultiPoint(a) assert_array_equal(geoma.context, array([[1., 2.], [3., 4.]])) self.assertEqual(dump_coords(geoma), [[(1.0, 2.0)], [(3.0, 4.0)]]) # Now, the inverse self.assertEqual(geoma.__array_interface__, geoma.context.__array_interface__) pas = asarray(geoma) assert_array_equal(pas, array([[1., 2.], [3., 4.]]))
def test_polygonize_full(self): lines2 = [ ((0, 0), (1, 1)), ((0, 0), (0, 1)), ((0, 1), (1, 1)), ((1, 1), (1, 0)), ((1, 0), (0, 0)), ((5, 5), (6, 6)), ((1, 1), (100, 100)), ] result2, dangles, cuts, invalids = polygonize_full(lines2) self.assertEqual(len(result2.geoms), 2) self.assertTrue(all([isinstance(x, Polygon) for x in result2.geoms])) self.assertEqual(list(dangles.geoms), []) self.assertTrue(all([isinstance(x, LineString) for x in cuts.geoms])) self.assertEqual(dump_coords(cuts), [[(1.0, 1.0), (100.0, 100.0)], [(5.0, 5.0), (6.0, 6.0)]]) self.assertEqual(list(invalids.geoms), [])
def test_polygonize_full(self): lines2 = [ ((0, 0), (1, 1)), ((0, 0), (0, 1)), ((0, 1), (1, 1)), ((1, 1), (1, 0)), ((1, 0), (0, 0)), ((5, 5), (6, 6)), ((1, 1), (100, 100)), ] result2, dangles, cuts, invalids = polygonize_full(lines2) self.assertEqual(len(result2), 2) self.assertTrue(all([isinstance(x, Polygon) for x in result2])) self.assertEqual(list(dangles.geoms), []) self.assertTrue(all([isinstance(x, LineString) for x in cuts.geoms])) self.assertEqual( dump_coords(cuts), [[(1.0, 1.0), (100.0, 100.0)], [(5.0, 5.0), (6.0, 6.0)]]) self.assertEqual(list(invalids.geoms), [])
def test_multipolygon(self): # From coordinate tuples coords = [ ( ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))], ) ] geom = MultiPolygon(coords) assert isinstance(geom, MultiPolygon) assert len(geom.geoms) == 1 assert dump_coords(geom) == [ [ (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)], ] ] # Or from polygons p = Polygon( ((0, 0), (0, 1), (1, 1), (1, 0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))], ) geom = MultiPolygon([p]) assert len(geom.geoms) == 1 assert dump_coords(geom) == [ [ (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)], ] ] # Or from another multi-polygon geom2 = MultiPolygon(geom) assert len(geom2.geoms) == 1 assert dump_coords(geom2) == [ [ (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)], ] ] # Sub-geometry Access assert isinstance(geom.geoms[0], Polygon) assert dump_coords(geom.geoms[0]) == [ (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)], ] with pytest.raises(IndexError): # index out of range geom.geoms[1] # Geo interface assert geom.__geo_interface__ == { "type": "MultiPolygon", "coordinates": [ ( ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)), ((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)), ) ], }
def test_multipoint_adapter(self): # Adapt a coordinate list to a line string coords = [[5.0, 6.0], [7.0, 8.0]] geoma = asMultiPoint(coords) self.assertEqual(dump_coords(geoma), [[(5.0, 6.0)], [(7.0, 8.0)]])
m.drawcountries(linewidth=0.7, color='black') m.drawcounties(linewidth=0.5, color='gray') #get roads if os.path.exists(homeDir + 'bin/' + rda + 'roads.pck') == False: roads = [] req = DataAccessLayer.newDataRequest(datatype='maps', table='mapdata.ak_highways', geomField='the_geom', parameters=['hwyname'], envelope=envelope) response = DataAccessLayer.getGeometryData(req) for hwy in response: roads.append({ 'name': hwy.getString('hwyname'), 'coords': shpbase.dump_coords(hwy.getGeometry()) }) #roads.append(shpbase.dump_coords(hwy.getGeometry())) pickle.dump(roads, open(homeDir + 'bin/' + rda + 'roads.pck', 'w')) else: roads = pickle.load(open(homeDir + 'bin/' + rda + 'roads.pck', 'r')) for hwy in roads: if hwy['name'][0] != '#': for line in hwy['coords']: lon = [] lat = [] for point in line: lon.append(point[0]) lat.append(point[1]) lons, lats = m(lon, lat)
def test_polygon(self): # Initialization # Linear rings won't usually be created by users, but by polygons coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)) ring = LinearRing(coords) self.assertEqual(len(ring.coords), 5) self.assertEqual(ring.coords[0], ring.coords[4]) self.assertEqual(ring.coords[0], ring.coords[-1]) self.assertTrue(ring.is_ring) # Coordinate modification ring.coords = ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0)) self.assertEqual( ring.__geo_interface__, { 'type': 'LinearRing', 'coordinates': ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)) }) # Test ring adapter coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]] ra = asLinearRing(coords) self.assertTrue(ra.wkt.upper().startswith('LINEARRING')) self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]) coords[3] = [2.0, -1.0] self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)]) # Construct a polygon, exterior ring only polygon = Polygon(coords) self.assertEqual(len(polygon.exterior.coords), 5) # Ring Access self.assertIsInstance(polygon.exterior, LinearRing) ring = polygon.exterior self.assertEqual(len(ring.coords), 5) self.assertEqual(ring.coords[0], ring.coords[4]) self.assertEqual(ring.coords[0], (0., 0.)) self.assertTrue(ring.is_ring) self.assertEqual(len(polygon.interiors), 0) # Create a new polygon from WKB data = polygon.wkb polygon = None ring = None polygon = load_wkb(data) ring = polygon.exterior self.assertEqual(len(ring.coords), 5) self.assertEqual(ring.coords[0], ring.coords[4]) self.assertEqual(ring.coords[0], (0., 0.)) self.assertTrue(ring.is_ring) polygon = None # Interior rings (holes) polygon = Polygon(coords, [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]) self.assertEqual(len(polygon.exterior.coords), 5) self.assertEqual(len(polygon.interiors[0].coords), 5) with self.assertRaises(IndexError): # index out of range polygon.interiors[1] # Test from another Polygon copy = Polygon(polygon) self.assertEqual(len(polygon.exterior.coords), 5) self.assertEqual(len(polygon.interiors[0].coords), 5) with self.assertRaises(IndexError): # index out of range polygon.interiors[1] # Coordinate getters and setters raise exceptions self.assertRaises(NotImplementedError, polygon._get_coords) with self.assertRaises(NotImplementedError): polygon.coords # Geo interface self.assertEqual( polygon.__geo_interface__, { 'type': 'Polygon', 'coordinates': (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)), ((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25))) }) # Adapter hole_coords = [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))] pa = asPolygon(coords, hole_coords) self.assertEqual(len(pa.exterior.coords), 5) self.assertEqual(len(pa.interiors), 1) self.assertEqual(len(pa.interiors[0].coords), 5) # Test Non-operability of Null rings r_null = LinearRing() self.assertEqual(r_null.wkt, 'GEOMETRYCOLLECTION EMPTY') self.assertEqual(r_null.length, 0.0) # Check that we can set coordinates of a null geometry r_null.coords = [(0, 0), (1, 1), (1, 0)] self.assertAlmostEqual(r_null.length, 3.414213562373095) # Error handling with self.assertRaises(ValueError): # A LinearRing must have at least 3 coordinate tuples Polygon([[1, 2], [2, 3]])
def test_multipoint_from_numpy(self): # Construct from a numpy array geom = MultiPoint(np.array([[0.0, 0.0], [1.0, 2.0]])) assert isinstance(geom, MultiPoint) assert len(geom.geoms) == 2 assert dump_coords(geom) == [[(0.0, 0.0)], [(1.0, 2.0)]]
def test_multipolygon(self): # From coordinate tuples geom = MultiPolygon( [(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])]) self.assertIsInstance(geom, MultiPolygon) self.assertEqual(len(geom.geoms), 1) self.assertEqual( dump_coords(geom), [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]]) # Or from polygons p = Polygon(((0, 0), (0, 1), (1, 1), (1, 0)), [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]) geom = MultiPolygon([p]) self.assertEqual(len(geom.geoms), 1) self.assertEqual( dump_coords(geom), [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]]) # Or from another multi-polygon geom2 = MultiPolygon(geom) self.assertEqual(len(geom2.geoms), 1) self.assertEqual( dump_coords(geom2), [[(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]]) # Sub-geometry Access self.assertIsInstance(geom.geoms[0], Polygon) self.assertEqual( dump_coords(geom.geoms[0]), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0), [(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)]]) with self.assertRaises(IndexError): # index out of range geom.geoms[1] # Geo interface self.assertEqual( geom.__geo_interface__, {'type': 'MultiPolygon', 'coordinates': [(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)), ((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)))]}) # Adapter coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)) holes_coords = [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))] mpa = asMultiPolygon([(coords, holes_coords)]) self.assertEqual(len(mpa.geoms), 1) self.assertEqual(len(mpa.geoms[0].exterior.coords), 5) self.assertEqual(len(mpa.geoms[0].interiors), 1) self.assertEqual(len(mpa.geoms[0].interiors[0].coords), 5)
def test_polygon(self): # Initialization # Linear rings won't usually be created by users, but by polygons coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)) ring = LinearRing(coords) self.assertEqual(len(ring.coords), 5) self.assertEqual(ring.coords[0], ring.coords[4]) self.assertEqual(ring.coords[0], ring.coords[-1]) self.assertTrue(ring.is_ring) # Coordinate modification ring.coords = ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0)) self.assertEqual( ring.__geo_interface__, {'type': 'LinearRing', 'coordinates': ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0))}) # Test ring adapter coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]] ra = asLinearRing(coords) self.assertTrue(ra.wkt.upper().startswith('LINEARRING')) self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]) coords[3] = [2.0, -1.0] self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)]) # Construct a polygon, exterior ring only polygon = Polygon(coords) self.assertEqual(len(polygon.exterior.coords), 5) # Ring Access self.assertIsInstance(polygon.exterior, LinearRing) ring = polygon.exterior self.assertEqual(len(ring.coords), 5) self.assertEqual(ring.coords[0], ring.coords[4]) self.assertEqual(ring.coords[0], (0., 0.)) self.assertTrue(ring.is_ring) self.assertEqual(len(polygon.interiors), 0) # Create a new polygon from WKB data = polygon.wkb polygon = None ring = None polygon = load_wkb(data) ring = polygon.exterior self.assertEqual(len(ring.coords), 5) self.assertEqual(ring.coords[0], ring.coords[4]) self.assertEqual(ring.coords[0], (0., 0.)) self.assertTrue(ring.is_ring) polygon = None # Interior rings (holes) polygon = Polygon(coords, [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]) self.assertEqual(len(polygon.exterior.coords), 5) self.assertEqual(len(polygon.interiors[0].coords), 5) with self.assertRaises(IndexError): # index out of range polygon.interiors[1] # Test from another Polygon copy = Polygon(polygon) self.assertEqual(len(polygon.exterior.coords), 5) self.assertEqual(len(polygon.interiors[0].coords), 5) with self.assertRaises(IndexError): # index out of range polygon.interiors[1] # Coordinate getters and setters raise exceptions self.assertRaises(NotImplementedError, polygon._get_coords) with self.assertRaises(NotImplementedError): polygon.coords # Geo interface self.assertEqual( polygon.__geo_interface__, {'type': 'Polygon', 'coordinates': (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)), ((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)))}) # Adapter hole_coords = [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))] pa = asPolygon(coords, hole_coords) self.assertEqual(len(pa.exterior.coords), 5) self.assertEqual(len(pa.interiors), 1) self.assertEqual(len(pa.interiors[0].coords), 5) # Test Non-operability of Null rings r_null = LinearRing() self.assertEqual(r_null.wkt, 'GEOMETRYCOLLECTION EMPTY') self.assertEqual(r_null.length, 0.0) # Check that we can set coordinates of a null geometry r_null.coords = [(0, 0), (1, 1), (1, 0)] self.assertAlmostEqual(r_null.length, 3.414213562373095) # Error handling with self.assertRaises(ValueError): # A LinearRing must have at least 3 coordinate tuples Polygon([[1, 2], [2, 3]])
def test_numpy(self): # Construct from a numpy array geom = MultiLineString([np.array(((0.0, 0.0), (1.0, 2.0)))]) assert isinstance(geom, MultiLineString) assert len(geom.geoms) == 1 assert dump_coords(geom) == [[(0.0, 0.0), (1.0, 2.0)]]