/usr/share/pyshared/imposm/geom.py is in python-imposm 2.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | # Copyright 2011 Omniscale (http://omniscale.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import shapely.geos
from shapely.geometry.base import BaseGeometry
from shapely import geometry
from shapely import wkt
import logging
log = logging.getLogger(__name__)
class InvalidGeometryError(Exception):
pass
class IncompletePolygonError(Exception):
pass
TOLERANCE_DEEGREES = 1e-8
TOLERANCE_METERS = 1e-3
# older versions had unhandled floating point execptions in .buffer(0)
SHAPELY_SUPPORTS_BUFFER = shapely.geos.geos_capi_version >= (1, 6, 0)
def validate_and_simplify(geom, meter_units=False):
if SHAPELY_SUPPORTS_BUFFER:
# buffer(0) is nearly fast as is_valid
return geom.buffer(0)
orig_geom = geom
if not geom.is_valid:
tolerance = TOLERANCE_METERS if meter_units else TOLERANCE_DEEGREES
geom = geom.simplify(tolerance, False)
if not geom.is_valid:
raise InvalidGeometryError('geometry is invalid, could not simplify: %s' %
orig_geom)
return geom
class GeomBuilder(object):
def build(self, osm_elem):
# TODO is this method still in use?
try:
if isinstance(osm_elem.coords, BaseGeometry):
return osm_elem.coords
geom_wkt = self.to_wkt(osm_elem.coords)
if geom_wkt is not None:
geom = wkt.loads(geom_wkt)
except Exception, ex:
raise InvalidGeometryError('unable to build geometry %s: %s %s' %
(osm_elem.osm_id, ex, osm_elem.coords))
if geom_wkt is None or geom is None:
# unable to build valid wkt (non closed polygon, etc)
raise InvalidGeometryError()
return geom
def check_geom_type(self, geom):
return
def build_geom(self, osm_elem):
try:
if isinstance(osm_elem.coords, BaseGeometry):
if osm_elem.coords.is_empty:
raise InvalidGeometryError('empty geometry')
self.check_geom_type(osm_elem.coords)
return osm_elem.coords
geom = self.to_geom(osm_elem.coords)
except InvalidGeometryError, ex:
raise InvalidGeometryError('unable to build geometry %s: %s %s' %
(osm_elem.osm_id, ex, osm_elem.coords))
if geom is None:
# unable to build valid wkt (non closed polygon, etc)
raise InvalidGeometryError()
return geom
class PointBuilder(GeomBuilder):
def to_wkt(self, data):
if len(data) != 2:
return None
return 'POINT(%f %f)' % data
def to_geom(self, data):
if len(data) != 2:
return None
return geometry.Point(*data)
def check_geom_type(self, geom):
if geom.type != 'Point':
raise InvalidGeometryError('expected Point, got %s' % geom.type)
def build_checked_geom(self, osm_elem, validate=False):
geom = self.build_geom(osm_elem)
if not validate or geom.is_valid:
return geom
else:
raise InvalidGeometryError('invalid geometry for %s: %s, %s' %
(osm_elem.osm_id, geom, osm_elem.coords))
class PolygonBuilder(GeomBuilder):
def to_wkt(self, data):
if len(data) >= 4 and data[0] == data[-1]:
return 'POLYGON((' + ', '.join('%f %f' % p for p in data) + '))'
return None
def to_geom(self, data):
if len(data) >= 4 and data[0] == data[-1]:
return geometry.Polygon(data)
return None
def check_geom_type(self, geom):
if geom.type not in ('Polygon', 'MultiPolygon'):
raise InvalidGeometryError('expected Polygon or MultiPolygon, got %s' % geom.type)
def build_checked_geom(self, osm_elem, validate=False):
geom = self.build_geom(osm_elem)
if not validate:
return geom
try:
return validate_and_simplify(geom)
except InvalidGeometryError:
raise InvalidGeometryError('invalid geometry for %s: %s, %s' %
(osm_elem.osm_id, geom, osm_elem.coords))
class LineStringBuilder(GeomBuilder):
def to_wkt(self, data):
if len(data) <= 1:
return None
if len(data) == 2 and data[0] == data[1]:
return None
return 'LINESTRING(' + ', '.join('%f %f' % p for p in data) + ')'
def check_geom_type(self, geom):
if geom.type != 'LineString':
raise InvalidGeometryError('expected LineString, got %s' % geom.type)
def to_geom(self, data):
if len(data) <= 1:
return None
if len(data) == 2 and data[0] == data[1]:
return None
return geometry.LineString(data)
def build_checked_geom(self, osm_elem, validate=False):
geom = self.build_geom(osm_elem)
if not validate or geom.is_valid:
return geom
else:
raise InvalidGeometryError('invalid geometry for %s: %s, %s' %
(osm_elem.osm_id, geom, osm_elem.coords))
|