Compare commits

..

5 Commits

3 changed files with 67 additions and 11 deletions

View File

@ -5,7 +5,14 @@ Module for rasterizing polygons, with float-precision anti-aliasing on
See the documentation for float_raster.raster(...) for details.
"""
from .float_raster import *
from .float_raster import (
raster as raster,
find_intersections as find_intersections,
create_vertices as create_vertices,
clip_vertices_to_window as clip_vertices_to_window,
get_raster_parts as get_raster_parts,
)
__author__ = 'Jan Petykiewicz'
__version__ = '0.7'

View File

@ -4,6 +4,11 @@ from numpy import logical_and, diff, floor, ceil, ones, zeros, hstack, full_like
from scipy import sparse
class FloatRasterError(Exception):
""" Custom exception for float_raster """
pass
def raster(
vertices: ArrayLike,
grid_x: ArrayLike,
@ -50,15 +55,15 @@ def raster(
def find_intersections(
vertices: NDArray[numpy.float_],
grid_x: NDArray[numpy.float_],
grid_y: NDArray[numpy.float_],
vertices: NDArray[numpy.floating],
grid_x: NDArray[numpy.floating],
grid_y: NDArray[numpy.floating],
) -> tuple[NDArray[numpy.float64], NDArray[numpy.float64], NDArray[numpy.float64]]:
"""
Find intersections between a polygon and grid lines
"""
if vertices.shape[0] != 2:
raise Exception('vertices must be 2xN')
raise FloatRasterError('vertices must be 2xN')
min_bounds = floor(vertices.min(axis=1))
max_bounds = ceil(vertices.max(axis=1))
@ -127,18 +132,18 @@ def find_intersections(
def create_vertices(
vertices: NDArray[numpy.float_],
grid_x: NDArray[numpy.float_],
grid_y: NDArray[numpy.float_],
vertices: NDArray[numpy.floating],
grid_x: NDArray[numpy.floating],
grid_y: NDArray[numpy.floating],
new_vertex_data: tuple[NDArray[numpy.float64], NDArray[numpy.float64], NDArray[numpy.float64]] | None = None
) -> sparse.coo_matrix:
"""
Create additional vertices where a polygon crosses gridlines
"""
if vertices.shape[0] != 2:
raise Exception('vertices must be 2xN')
raise FloatRasterError('vertices must be 2xN')
if grid_x.size < 1 or grid_y.size < 1:
raise Exception('Grid must contain at least one line in each direction?')
raise FloatRasterError('Grid must contain at least one line in each direction?')
num_poly_vertices = vertices.shape[1]
@ -235,7 +240,7 @@ def get_raster_parts(
grid_y = numpy.array(grid_y)
if grid_x.size < 2 or grid_y.size < 2:
raise Exception('Grid must contain at least one full pixel')
raise FloatRasterError('Grid must contain at least one full pixel')
num_xy_px = numpy.array([grid_x.size, grid_y.size]) - 1

View File

@ -14,6 +14,9 @@ homepage = "https://mpxd.net/code/jan/float_raster"
repository = "https://mpxd.net/code/jan/float_raster"
keywords = [
"coverage",
"raster",
"anti-alias",
"polygon",
]
classifiers = [
"Programming Language :: Python :: 3",
@ -36,3 +39,44 @@ dependencies = [
[tool.hatch.version]
path = "float_raster/__init__.py"
[tool.ruff]
exclude = [
".git",
"dist",
]
line-length = 145
indent-width = 4
lint.dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
lint.select = [
"NPY", "E", "F", "W", "B", "ANN", "UP", "SLOT", "SIM", "LOG",
"C4", "ISC", "PIE", "PT", "RET", "TCH", "PTH", "INT",
"ARG", "PL", "R", "TRY",
"G010", "G101", "G201", "G202",
"Q002", "Q003", "Q004",
]
lint.ignore = [
#"ANN001", # No annotation
"ANN002", # *args
"ANN003", # **kwargs
"ANN401", # Any
"ANN101", # self: Self
"SIM108", # single-line if / else assignment
"RET504", # x=y+z; return x
"PIE790", # unnecessary pass
"ISC003", # non-implicit string concatenation
"C408", # dict(x=y) instead of {'x': y}
"PLR09", # Too many xxx
"PLR2004", # magic number
"PLC0414", # import x as x
"TRY003", # Long exception message
]
[[tool.mypy.overrides]]
module = [
"scipy",
"scipy.sparse",
]
ignore_missing_imports = true