1- from numpy import number
21from metacity .utils .filesystem import read_json
32from metacity .geometry import Attribute , Model , Progress
43from typing import List , Union
5- from pyproj import Proj , Transformer
64
75
86__all__ = ["parse" , "parse_data" ]
97
108
11-
12- class Projector :
13- def __init__ (self , from_crs : str = None , to_crs : str = None ):
14- self .from_crs = from_crs
15- self .to_crs = to_crs
16- self .transform = None
17-
18- if from_crs is None or to_crs is None :
19- return
20-
21- proj_from = Proj (from_crs , preserve_units = False )
22- proj_to = Proj (to_crs , preserve_units = False )
23- self .transform = Transformer .from_proj (proj_from , proj_to )
24-
25- def project (self , coordinates : List [List [float ]]):
26- if self .transform is None :
27- return coordinates
28- return [ p for p in self .transform .itransform (coordinates ) ]
29-
30-
319class Geometry :
3210 def __init__ (self , data ):
3311 self .data = data
34- self .transform = None
3512
3613 @property
3714 def dim (self ):
@@ -44,16 +21,6 @@ def dim(self):
4421 f"Encountered primitive with unsupported dimension { d } " )
4522 return d
4623
47- def set_projection (self , projector : Projector ):
48- if projector is None :
49- return
50- self .transform = projector .transform
51-
52- def project (self , coordinates : List [List [float ]]):
53- if self .transform is None :
54- return coordinates
55- return [ p for p in self .transform .itransform (coordinates ) ]
56-
5724 @property
5825 def coordinates (self ):
5926 return self .data ['coordinates' ]
@@ -93,26 +60,23 @@ def to_model(attr: Attribute):
9360###############################################################################
9461
9562
96- def parse_point (g : Geometry , attr : Attribute , dim : int , vertices : List [List [float ]]):
97- vertices = g .project (vertices )
63+ def parse_point (attr : Attribute , dim : int , vertices : List [List [float ]]):
9864 vertices = flatten (vertices )
9965 if dim == 2 :
10066 attr .push_point2D (vertices )
10167 elif dim == 3 :
10268 attr .push_point3D (vertices )
10369
10470
105- def parse_line (g : Geometry , attr : Attribute , dim : int , line : List [List [float ]]):
106- l = g .project (line )
71+ def parse_line (attr : Attribute , dim : int , line : List [List [float ]]):
10772 l = flatten (line )
10873 if dim == 2 :
10974 attr .push_line2D (l )
11075 elif dim == 3 :
11176 attr .push_line3D (l )
11277
11378
114- def parse_polygon (g : Geometry , attr : Attribute , dim : int , polygon : List [List [List [float ]]]):
115- polygon = [ g .project (p ) for p in polygon ]
79+ def parse_polygon (attr : Attribute , dim : int , polygon : List [List [List [float ]]]):
11680 p = flatten_polygon (polygon )
11781 if dim == 2 :
11882 attr .push_polygon2D (p )
@@ -126,44 +90,44 @@ def parse_polygon(g: Geometry, attr: Attribute, dim: int, polygon: List[List[Lis
12690def attr_from_point (geometry : Geometry ):
12791 attr = Attribute ()
12892 vertices = [geometry .coordinates ]
129- parse_point (geometry , attr , geometry .dim , vertices )
93+ parse_point (attr , geometry .dim , vertices )
13094 return [attr ]
13195
13296
13397def attr_from_multipoint (geometry : Geometry ):
13498 attr = Attribute ()
13599 vertices = geometry .coordinates
136- parse_point (geometry , attr , geometry .dim , vertices )
100+ parse_point (attr , geometry .dim , vertices )
137101 return [attr ]
138102
139103
140104def attr_from_linestring (geometry : Geometry ):
141105 attr = Attribute ()
142106 vertices = geometry .coordinates
143- parse_line (geometry , attr , geometry .dim , vertices )
107+ parse_line (attr , geometry .dim , vertices )
144108 return [attr ]
145109
146110
147111def attr_from_multilinestring (geometry : Geometry ):
148112 attr = Attribute ()
149113 dim = geometry .dim
150114 for line in geometry .coordinates :
151- parse_line (geometry , attr , dim , line )
115+ parse_line (attr , dim , line )
152116 return [attr ]
153117
154118
155119def attr_from_polygon (geometry : Geometry ):
156120 attr = Attribute ()
157121 vertices = geometry .coordinates
158- parse_polygon (geometry , attr , geometry .dim , vertices )
122+ parse_polygon (attr , geometry .dim , vertices )
159123 return [attr ]
160124
161125
162126def attr_from_multipolygon (geometry : Geometry ):
163127 attr = Attribute ()
164128 dim = geometry .dim
165129 for polygon in geometry .coordinates :
166- parse_polygon (geometry , attr , dim , polygon )
130+ parse_polygon (attr , dim , polygon )
167131 return [attr ]
168132
169133
@@ -188,8 +152,7 @@ def attr_from_geometrycollection(geometry: Geometry):
188152}
189153
190154
191- def parse_geometry (feature : Feature , projector : Projector ):
192- feature .geometry .set_projection (projector )
155+ def parse_geometry (feature : Feature ):
193156 if feature .geometry .geometry_type not in typedict :
194157 raise Exception (f"Unknown geometry type { feature .geometry .geometry_type } " )
195158 attr_list = typedict [feature .geometry .geometry_type ](feature .geometry )
@@ -207,23 +170,22 @@ def parse_models(feature: Feature, attr_list: List[Attribute], progress: Union[P
207170 return models
208171
209172
210- def parse_feature (feature : Feature , projector : Projector , progress : Progress ):
211- attr_list = parse_geometry (feature , projector )
173+ def parse_feature (feature : Feature , progress : Progress ):
174+ attr_list = parse_geometry (feature )
212175 models = parse_models (feature , attr_list , progress )
213176 return models
214177
215178
216- def parse_data (data , from_crs : str , to_crs : str , progress : Progress = None ):
179+ def parse_data (data , progress : Progress = None ):
217180 models = []
218- projector = Projector (from_crs , to_crs )
219181 for f in data ['features' ]:
220182 feature = Feature (f )
221- models .extend (parse_feature (feature , projector , progress ))
183+ models .extend (parse_feature (feature , progress ))
222184 return models
223185
224186
225- def parse (input_file : str , from_crs : str = None , to_crs : str = None , progress : Progress = None ):
187+ def parse (input_file : str , progress : Progress = None ):
226188 contents = read_json (input_file )
227- return parse_data (contents , from_crs , to_crs , progress )
189+ return parse_data (contents , progress )
228190
229191
0 commit comments