def test_st_exterior_ring(self): polygon_df = create_simple_polygons_df(self.spark, 5) additional_wkt = "POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))" additional_wkt_df = self.spark.createDataFrame( [[wkt.loads(additional_wkt)]], self.geo_schema) polygons_df = polygon_df.union(additional_wkt_df) other_geometry_df = create_sample_lines_df(self.spark, 5).union( create_sample_points_df(self.spark, 5)) linestring_df = polygons_df.selectExpr( "ST_ExteriorRing(geom) as geom").filter("geom IS NOT NULL") empty_df = other_geometry_df.selectExpr( "ST_ExteriorRing(geom) as geom").filter("geom IS NOT NULL") linestring_wkt = [ wkt_row[0] for wkt_row in linestring_df.selectExpr( "ST_AsText(geom)").collect() ] assert (linestring_wkt == [ "LINESTRING (0 0, 0 1, 1 1, 1 0, 0 0)", "LINESTRING (0 0, 1 1, 1 2, 1 1, 0 0)" ]) assert (not empty_df.count())
def test_st_start_point(self): point_df = create_sample_points_df(self.spark, 5) polygon_df = create_sample_polygons_df(self.spark, 5) linestring_df = create_sample_lines_df(self.spark, 5) expected_points = [ "POINT (-112.506968 45.98186)", "POINT (-112.519856 45.983586)", "POINT (-112.504872 45.919281)", "POINT (-112.574945 45.987772)", "POINT (-112.520691 42.912313)" ] points = point_df.selectExpr("ST_StartPoint(geom) as geom").filter( "geom IS NOT NULL") polygons = polygon_df.selectExpr("ST_StartPoint(geom) as geom").filter( "geom IS NOT NULL") linestrings = linestring_df.selectExpr( "ST_StartPoint(geom) as geom").filter("geom IS NOT NULL") assert ([line[0] for line in linestrings.collect() ] == [wkt.loads(el) for el in expected_points]) assert (not points.count()) assert (not polygons.count())
def test_st_interior_ring_n(self): polygon_df = self.__wkt_list_to_data_frame( ["POLYGON((0 0, 0 5, 5 5, 5 0, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1), (1 3, 2 3, 2 4, 1 4, 1 3), (3 3, 4 3, 4 4, 3 4, 3 3))"] ) other_geometry = create_sample_points_df(self.spark, 5).union(create_sample_lines_df(self.spark, 5)) wholes = [polygon_df.selectExpr(f"ST_InteriorRingN(geom, {i}) as geom"). selectExpr("ST_AsText(geom)").collect()[0][0] for i in range(3)] empty_df = other_geometry.selectExpr("ST_InteriorRingN(geom, 1) as geom").filter("geom IS NOT NULL") assert(not empty_df.count()) assert(wholes == ["LINESTRING (1 1, 2 1, 2 2, 1 2, 1 1)", "LINESTRING (1 3, 2 3, 2 4, 1 4, 1 3)", "LINESTRING (3 3, 4 3, 4 4, 3 4, 3 3)"])
def test_st_y(self): point_df = create_sample_points_df(self.spark, 5) polygon_df = create_sample_polygons_df(self.spark, 5) linestring_df = create_sample_lines_df(self.spark, 5) points = point_df \ .selectExpr("ST_Y(geom)").collect() polygons = polygon_df.selectExpr("ST_Y(geom) as y").filter("y IS NOT NULL") linestrings = linestring_df.selectExpr("ST_Y(geom) as y").filter("y IS NOT NULL") assert([point[0] for point in points] == [42.28787, 32.324142, 32.324142, 5.3324324, -88.331492]) assert(not linestrings.count()) assert(not polygons.count())
def test_st_x(self): point_df = create_sample_points_df(self.spark, 5) polygon_df = create_sample_polygons_df(self.spark, 5) linestring_df = create_sample_lines_df(self.spark, 5) points = point_df \ .selectExpr("ST_X(geom)").collect() polygons = polygon_df.selectExpr("ST_X(geom) as x").filter("x IS NOT NULL") linestrings = linestring_df.selectExpr("ST_X(geom) as x").filter("x IS NOT NULL") assert([point[0] for point in points] == [-71.064544, -88.331492, 88.331492, 1.0453, 32.324142]) assert(not linestrings.count()) assert(not polygons.count())
def test_st_dump_points(self): expected_points = [ "POINT (-112.506968 45.98186)", "POINT (-112.506968 45.983586)", "POINT (-112.504872 45.983586)", "POINT (-112.504872 45.98186)", "POINT (-71.064544 42.28787)", "POINT (0 0)", "POINT (0 1)", "POINT (1 1)", "POINT (1 0)", "POINT (0 0)" ] geometry_df = create_sample_lines_df(self.spark, 1) \ .union(create_sample_points_df(self.spark, 1)) \ .union(create_simple_polygons_df(self.spark, 1)) dumped_points = geometry_df.selectExpr("ST_DumpPoints(geom) as geom") \ .select(explode(col("geom")).alias("geom")) assert (dumped_points.count() == 10) collected_points = [ geom_row[0] for geom_row in dumped_points.selectExpr( "ST_AsText(geom)").collect() ] assert (collected_points == expected_points)
def test_st_end_point(self): linestring_dataframe = create_sample_lines_df(self.spark, 5) other_geometry_dataframe = create_sample_points_df(self.spark, 5). \ union(create_sample_points_df(self.spark, 5)) point_data_frame = linestring_dataframe.selectExpr("ST_EndPoint(geom) as geom"). \ filter("geom IS NOT NULL") expected_ending_points = [ "POINT (-112.504872 45.98186)", "POINT (-112.506968 45.983586)", "POINT (-112.41643 45.919281)", "POINT (-112.519856 45.987772)", "POINT (-112.442664 42.912313)" ] empty_dataframe = other_geometry_dataframe.selectExpr("ST_EndPoint(geom) as geom"). \ filter("geom IS NOT NULL") assert ([ wkt_row[0] for wkt_row in point_data_frame.selectExpr( "ST_AsText(geom)").collect() ] == expected_ending_points) assert (empty_dataframe.count() == 0)