欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

矩形拉长

程序员文章站 2022-04-03 16:52:47
...
CREATE OR REPLACE FUNCTION "public"."mbk_st_iphone20"("geom" "public"."geometry")
  RETURNS "public"."geometry" AS $BODY$
DECLARE
    temp geometry;
		pCount int;
		startIndex int;
		latDiff float8;
		lonDiff float8;
		newPoints geometry[9];
		
    result geometry;
begin
    if $1 is not null
    then
				temp := $1;
				FOR i IN 1..40 LOOP
						pCount := ST_NPoints(temp);
						IF pCount <= 5 OR pCount > 50 THEN
								EXIT;
						END IF;
						temp := st_simplify(temp,0.0000025 * i);
				END LOOP;
				pCount := ST_NPoints(temp);
				if pCount = 5
				then
						BEGIN
								temp := ST_Boundary(temp);
								if st_length(st_makeline(ST_PointN(temp,1), ST_PointN(temp,2))) > st_length(st_makeline(ST_PointN(temp,2), ST_PointN(temp,3)))
								then startIndex := 2;
								else startIndex := 1;
								end if;
								latDiff := ((ST_Y(ST_PointN(temp,startIndex))-ST_Y(ST_PointN(temp,startIndex+3))) + (ST_Y(ST_PointN(temp,startIndex+1))-ST_Y(ST_PointN(temp,startIndex+2))))/2;
								lonDiff := ((ST_X(ST_PointN(temp,startIndex+3))-ST_X(ST_PointN(temp,startIndex))) + (ST_X(ST_PointN(temp,startIndex+2))-ST_X(ST_PointN(temp,startIndex+1))))/2;
			
								latDiff := latDiff * 50;
								lonDiff := lonDiff * 50;
								newPoints[0] := st_setsrid(ST_PointN(temp,startIndex),4326);
								newPoints[1] := st_setsrid(st_point(ST_X(ST_PointN(temp,startIndex)) - lonDiff, ST_Y(ST_PointN(temp,startIndex)) + latDiff),4326);
								newPoints[2] := st_setsrid(st_point(ST_X(ST_PointN(temp,startIndex+1)) - lonDiff, ST_Y(ST_PointN(temp,startIndex+1)) + latDiff),4326);
								newPoints[3] := st_setsrid(ST_PointN(temp,startIndex+1),4326);
								newPoints[4] := st_setsrid(ST_PointN(temp,startIndex+2),4326);
								newPoints[5] := st_setsrid(st_point(ST_X(ST_PointN(temp,startIndex+2)) + lonDiff, ST_Y(ST_PointN(temp,startIndex+2)) - latDiff),4326);
								newPoints[6] := st_setsrid(st_point(ST_X(ST_PointN(temp,startIndex+3)) + lonDiff, ST_Y(ST_PointN(temp,startIndex+3)) - latDiff),4326);
								newPoints[7] := st_setsrid(ST_PointN(temp,startIndex+3),4326);
								newPoints[8] := newPoints[0];
								
								result := st_setsrid(st_makepolygon(st_makeline(newPoints)),4326);
								if not ST_IsValid(result)
								then result := null;
								end IF;
						EXCEPTION
								WHEN others THEN
								raise notice '%', ST_PointN(temp,startIndex);
						END;
				end if;
    end if;
    return result;
end
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
相关标签: postgresql 矩形