def writeEvaluationFile(self): opgeojson = self.systemname + '.geojson' cwd = os.getcwd() outputdirectory = os.path.join(cwd,config.settings['outputdirectory']) if not os.path.exists(outputdirectory): os.mkdir(outputdirectory) opfile = os.path.join(outputdirectory, opgeojson) fc = {"type":"FeatureCollection", "features":[]} for color, colorfeatures in self.colorDict.items(): for curcolorfeature in colorfeatures: # print curcolorfeature f = json.loads(ShapelyHelper.export_to_JSON(curcolorfeature)) f['properties']={} f['properties']['areatype'] = color.lower() fc['features'].append(f) if self.symdifference: geom_type= self.symdifference.geom_type if geom_type =='MultiPolygon': for geom in self.symdifference.geoms: symdiffindfeat = ShapelyHelper.export_to_JSON(geom) tmpf = {'type':'Feature','properties':{'areatype':'yellow'}, 'geometry':json.loads(symdiffindfeat)} fc['features'].append((tmpf)) elif geom_type == 'Polygon': symdiffindfeat = ShapelyHelper.export_to_JSON(self.symdifference) tmpf = {'type':'Feature','properties':{'areatype':'yellow'}, 'geometry':json.loads(symdiffindfeat)} fc['features'].append(tmpf) with open(opfile, 'w') as output_evaluation: output_evaluation.write(json.dumps(fc))
def dissolveColors(self): colorDict = self.colorDict try: cd = {} for color, colorFeatures in colorDict.items(): allExistingcolorfeatures = [] for curcolorfeature in colorFeatures: try: s = asShape(curcolorfeature['geometry']) except Exception as e: pass else: if s.is_valid: allExistingcolorfeatures.append(s) allExistingcolorfeaturesUnion = unary_union(allExistingcolorfeatures) if allExistingcolorfeaturesUnion.geom_type == 'MultiPolygon': tmpfs = [] allExistingColorUnion = [polygon for polygon in allExistingcolorfeaturesUnion] for existingUnionPolygon in allExistingColorUnion: g = json.loads(ShapelyHelper.export_to_JSON(existingUnionPolygon)) tmpf = {'type':'Feature','properties':{'areatype':color}, 'geometry':g } tmpfs.append(tmpf) cd[color] = tmpfs else: g = json.loads(ShapelyHelper.export_to_JSON(allExistingcolorfeaturesUnion)) tmpf = {'type':'Feature','properties':{'areatype':color}, 'geometry':g } cd[color] = [tmpf] except Exception as e: # if the unary union fails fail gracefully pass else: self.colorDict = cd
def writeEvaluationFile(self): opgeojson = self.systemname + '.geojson' cwd = os.getcwd() outputdirectory = os.path.join(cwd, config.settings['outputdirectory']) if not os.path.exists(outputdirectory): os.mkdir(outputdirectory) opfile = os.path.join(outputdirectory, opgeojson) fc = {"type": "FeatureCollection", "features": []} for color, colorfeatures in self.colorDict.items(): for curcolorfeature in colorfeatures: # print curcolorfeature f = json.loads(ShapelyHelper.export_to_JSON(curcolorfeature)) f['properties'] = {} f['properties']['areatype'] = color.lower() fc['features'].append(f) with open(opfile, 'w') as output_evaluation: output_evaluation.write(json.dumps(fc))
def processFile(self, color, rawfiledetails, propertyrules): curfeatures = self.colorDict[color] allfields = [] curfeatures = [] for k, v in propertyrules.items(): allfields.append(k.lower()) with fiona.open(rawfiledetails['location']) as source: for feature in source: try: for curfield in allfields: if feature['properties'][curfield] in propertyrules[ curfield.lower()]: if rawfiledetails['type'] == 'points': props = feature['properties'] try: pt = asShape(feature['geometry']) poly = pt.buffer(0.0004) except Exception as e: pass else: if pt.is_valid: poly = pt.buffer(0.0004) bufferedpoly = json.loads( ShapelyHelper.export_to_JSON(poly)) feature['geometry'] = bufferedpoly curfeatures.append( {'geometry': bufferedpoly}) elif rawfiledetails['type'] == 'lines': props = feature['properties'] try: pt = asShape(feature['geometry']) pt.is_valid except Exception as e: pass else: if pt.is_valid: poly = pt.buffer(0.0002) bufferedpoly = json.loads( ShapelyHelper.export_to_JSON(poly)) feature['geometry'] = bufferedpoly curfeatures.append( {'geometry': bufferedpoly}) else: props = feature['properties'] try: poly = asShape(feature['geometry']) poly.is_valid except Exception as e: pass else: if poly.is_valid: try: poly = poly.buffer(0.0004) except Exception as e: pass else: bufferedpoly = json.loads( ShapelyHelper.export_to_JSON( poly)) feature['geometry'] = bufferedpoly curfeatures.append( {'geometry': bufferedpoly}) except KeyError as ke: pass self.colorDict[color] = curfeatures