def write_centers_stops(self): '''get centers in radius around project centroid, write their closest stops and the stops near the project to the db ''' # truncate tables, will be filled in progress self.parent_tbx.delete_rows_in_table('Zentrale_Orte') self.parent_tbx.delete_rows_in_table('Haltestellen') centroid = get_project_centroid(self.par.projectname.value) df_central = self.parent_tbx.table_to_dataframe( 'Zentrale_Orte_Neu', workspace='FGDB_Basisdaten_deutschland.gdb', columns=['SHAPE', 'OBJECTID', 'GEN', 'OZ'], is_base_table=True) df_oz = df_central[df_central['OZ'] == 1] df_mz = df_central[df_central['OZ'] == 0] oz_points = df_oz['SHAPE'].values mz_points = df_mz['SHAPE'].values oz_points, oz_within = points_within(centroid, oz_points, radius=70000) mz_points, mz_within = points_within(centroid, mz_points, radius=30000) df_oz_within = df_oz[oz_within] df_mz_within = df_mz[mz_within] def get_closest_stops(points): stops = [] for point in points: t_p = Point(point[0], point[1], epsg=self.parent_tbx.config.epsg) t_p.transform(4326) stops_near = self.query.stops_near(t_p, n=1) if len(stops_near) > 0: closest = stops_near[0] stops.append(closest) return stops oz_stops = get_closest_stops(oz_points) mz_stops = get_closest_stops(mz_points) if (len(oz_stops) + len(mz_stops)) == 0: return df_oz_within['id_haltestelle'] = [s.id for s in oz_stops] df_mz_within['id_haltestelle'] = [s.id for s in mz_stops] df_within = pd.concat([df_oz_within, df_mz_within]) df_within['name'] = df_within['GEN'] df_within['id_zentraler_ort'] = df_within['OBJECTID'] self.parent_tbx.insert_dataframe_in_table('Zentrale_Orte', df_within) p_centroid = Point(centroid[0], centroid[1], epsg=self.parent_tbx.config.epsg) p_centroid.transform(4326) tfl_stops = self.query.stops_near(p_centroid, n=10) self._stops_to_db(oz_stops) self._stops_to_db(mz_stops) self._stops_to_db(tfl_stops, is_project_stop=1)
def get_bbox(self, table_path): desc = arcpy.Describe(table_path) epsg = self.parent_tbx.config.epsg ex = desc.extent bbox = (Point(ex.XMin, ex.YMin, epsg=epsg), Point(ex.XMax, ex.YMax, epsg=epsg)) return bbox
def add_bbox_edge(self, bbox, rel_edge=0.1): """ Change the size of the bbox to avoid rounding errors Parameters ---------- bbox : tuple of points p1 and p2 define the upper right and the lower left corner of the box rel_edge : float opt relative factor for enlargement Returns ------- bbox : tuple of points bigger bbox """ p1, p2 = bbox bbox_size = abs(p1.x-p2.x) edge = bbox_size * rel_edge p1_new = Point(p1.x - edge, p1.y - edge, epsg=p1.epsg) p2_new = Point(p2.x + edge, p2.y + edge, epsg=p2.epsg) ret = (p1_new, p2_new) return ret
def write_centers_stops(self): '''get centers in radius around project centroid, write their closest stops and the stops near the project to the db ''' # truncate tables, will be filled in progress self.parent_tbx.delete_rows_in_table('Zentrale_Orte') self.parent_tbx.delete_rows_in_table('Haltestellen') centroid = get_project_centroid(self.par.projectname.value) df_central = self.parent_tbx.table_to_dataframe( 'Zentrale_Orte_Neu', workspace='FGDB_Basisdaten_deutschland.gdb', columns=['SHAPE', 'OBJECTID', 'GEN', 'OZ'], is_base_table=True ) df_oz = df_central[df_central['OZ'] == 1] df_mz = df_central[df_central['OZ'] == 0] oz_points = df_oz['SHAPE'].values mz_points = df_mz['SHAPE'].values oz_points, oz_within = points_within(centroid, oz_points, radius=70000) mz_points, mz_within = points_within(centroid, mz_points, radius=30000) df_oz_within = df_oz[oz_within] df_mz_within = df_mz[mz_within] def get_closest_stops(points): stops = [] for point in points: t_p = Point(point[0], point[1], epsg=self.parent_tbx.config.epsg) t_p.transform(4326) stops_near = self.query.stops_near(t_p, n=1) if len(stops_near) > 0: closest = stops_near[0] stops.append(closest) return stops oz_stops = get_closest_stops(oz_points) mz_stops = get_closest_stops(mz_points) if (len(oz_stops) + len(mz_stops)) == 0: return df_oz_within['id_haltestelle'] = [s.id for s in oz_stops] df_mz_within['id_haltestelle'] = [s.id for s in mz_stops] df_within = pd.concat([df_oz_within, df_mz_within]) df_within['name'] = df_within['GEN'] df_within['id_zentraler_ort'] = df_within['OBJECTID'] self.parent_tbx.insert_dataframe_in_table('Zentrale_Orte', df_within) p_centroid = Point(centroid[0], centroid[1], epsg=self.parent_tbx.config.epsg) p_centroid.transform(4326) tfl_stops = self.query.stops_near(p_centroid, n=10) self._stops_to_db(oz_stops) self._stops_to_db(mz_stops) self._stops_to_db(tfl_stops, is_project_stop=1)
def get_closest_stops(points): stops = [] for point in points: t_p = Point(point[0], point[1], epsg=self.parent_tbx.config.epsg) t_p.transform(4326) stops_near = self.query.stops_near(t_p, n=1) if len(stops_near) > 0: closest = stops_near[0] stops.append(closest) return stops
def load(self, raster_file, unreachable=120): desc = arcpy.Describe(raster_file) e = desc.Extent self.srid = desc.spatialReference.factoryCode self.raster_origin = Point(e.XMin, e.YMax, epsg=self.srid) self.cellWidth = float(arcpy.GetRasterProperties_management( raster_file, 'CELLSIZEX').getOutput(0).replace(',', '.')) self.cellHeight = float(arcpy.GetRasterProperties_management( raster_file, 'CELLSIZEY').getOutput(0).replace(',', '.')) self.raster_values = dilate_raster( arcpy.RasterToNumPyArray(raster_file), threshold=unreachable )
def run(self): query = GeoserverQuery() radius = self.par.radius.value * 1000 x, y = get_project_centroid(self.par.projectname.value) centroid = Point(x, y, epsg=self.parent_tbx.config.epsg) target_epsg = self.parent_tbx.config.epsg arcpy.AddMessage('Frage Geoserver an...') features = query.get_features(centroid, radius, self.categories, target_epsg) arcpy.AddMessage('Schreibe {} Einrichtungen in die Datenbank...' .format(len(features))) self.parent_tbx.delete_rows_in_table('Einrichtungen') column_values = {'name': [], 'SHAPE': [], 'projektcheck_category': []} for feat in features: column_values['name'].append(feat.name) feat.create_geom() column_values['SHAPE'].append(feat.geom) column_values['projektcheck_category'].append(feat.category) self.parent_tbx.insert_rows_in_table('Einrichtungen', column_values)
def run(self): table = 'Isochrone' self.cutoff = self.par.cutoff.value cutoff_sec = self.cutoff * 60 column_values = { 'modus': [], 'SHAPE@': [], 'cutoff': [self.cutoff] * len(self.modes) } x, y = get_project_centroid(self.par.projectname.value) centroid = Point(x, y, epsg=self.parent_tbx.config.epsg) query = RoutingQuery() target_epsg = self.parent_tbx.config.epsg for mode, (name, walk_speed) in self.modes.iteritems(): arcpy.AddMessage( u'Ermittle die Isochronen für den Modus "{}"'.format(name)) iso_poly = query.get_isochrone(centroid, target_epsg, mode, cutoff_sec, walk_speed) column_values['modus'].append(name) column_values['SHAPE@'].append(iso_poly) arcpy.AddMessage('Schreibe die Isochronen in die Datenbank...') self.parent_tbx.delete_rows_in_table(table) self.parent_tbx.insert_rows_in_table(table, column_values)
def calculate_distances(self): '''calculate distances between settlement points and markets and write them to the database''' # calculate bounding box bbox = self.get_bbox(self.folders.get_table('Siedlungszellen')) markets = self.parent_tbx.table_to_dataframe('Maerkte') routing = DistanceRouting() destinations = self.get_cells() dest_ids = [d.id for d in destinations] already_calculated = np.unique( self.parent_tbx.table_to_dataframe('Beziehungen_Maerkte_Zellen', columns=['id_markt' ])['id_markt']) n_markets = len(markets) i = 1 for index, market in markets.iterrows(): arcpy.AddMessage(u' - {name} ({i}/{n})'.format(name=market['name'], i=i, n=n_markets)) i += 1 if market['id'] not in already_calculated: arcpy.AddMessage(' wird berechnet') market_id = market['id'] x, y = market['SHAPE'] origin = Point(x, y, id=market_id, epsg=self.parent_tbx.config.epsg) distances = routing.get_distances(origin, destinations, bbox) #distances = routing.get_distances(origin, destinations) arcpy.AddMessage(' wird gespeichert') self.distances_to_db(market_id, destinations, distances) else: arcpy.AddMessage(u' bereits berechnet, wird übersprungen') gc.collect()
def communities_to_centers(self, centroid, radius): '''get communities intersecting with bbox and write them as centers to the database''' import time start = time.time() vg = self.parent_tbx.folders.get_base_table( table='Verwaltungsgemeinschaften_dissolved', workspace='FGDB_Basisdaten_deutschland.gdb') gemeinden = self.parent_tbx.folders.get_base_table( table='bkg_gemeinden', workspace='FGDB_Basisdaten_deutschland.gdb') centroid = Point(centroid[0], centroid[1]) # circular buffer for clipping centroid.create_geom() pntGeom = arcpy.PointGeometry(centroid.geom) circleGeom = pntGeom.buffer(radius) fc_bbox = 'in_memory/bbox' fc_clipped = 'in_memory/clipped' if arcpy.Exists(fc_bbox): arcpy.Delete_management(fc_bbox) if arcpy.Exists(fc_clipped): arcpy.Delete_management(fc_clipped) arcpy.CopyFeatures_management([circleGeom], fc_bbox) arcpy.Clip_analysis(vg, fc_bbox, fc_clipped) cursor = arcpy.da.SearchCursor(fc_clipped, ['SHAPE@', 'GEN', 'RS']) id = 1 rs_list = [] # add clipped vg as centers for shape, name, rs in cursor: rs_list.append(rs) c2 = arcpy.da.SearchCursor(vg, ['SHAPE@'], where_clause=''' "RS"='{}' '''.format(rs)) shape = c2.next()[0] del(c2) self.parent_tbx.insert_rows_in_table( 'Zentren', workspace='FGDB_Standortkonkurrenz_Supermaerkte.gdb', column_values={ 'SHAPE@': shape, 'name': name, 'nutzerdefiniert': -1, # -1 indicates that it is a vg for selection and output only 'umsatz_differenz': 0, 'umsatz_planfall': 0, 'umsatz_nullfall': 0, 'id': id, 'Auswahl': 0, 'AGS': '', 'RS': rs }) id += 1 del cursor arcpy.Delete_management(fc_bbox) arcpy.Delete_management(fc_clipped) rs_project = None cursor = arcpy.da.SearchCursor(gemeinden, ['SHAPE@', 'GEN', 'AGS', 'RS']) for shape, name, ags, rs in cursor: cut_rs = rs[:9] if cut_rs in rs_list: if ags == self._project_ags: rs_project = cut_rs self.parent_tbx.insert_rows_in_table( 'Zentren', workspace='FGDB_Standortkonkurrenz_Supermaerkte.gdb', column_values={ 'SHAPE@': shape, 'name': name, 'nutzerdefiniert': 0, # 0 indicates gemeinden, for calculations only 'umsatz_differenz': 0, 'umsatz_planfall': 0, 'umsatz_nullfall': 0, 'id': id, 'Auswahl': 0, 'AGS': ags, 'RS': cut_rs, } ) id += 1 # preselect the VG the project is in if rs_project: self.parent_tbx.update_table( 'Zentren', {'Auswahl': -1}, # -1 : not unselectable where='"RS" = \'{}\''.format(rs_project), workspace='FGDB_Standortkonkurrenz_Supermaerkte.gdb') print('Dauer: {}'.format(time.time() - start))
return n_features = len(features) supermarkets = [] id_markt = 0 for feature in features: id_markt += 1 x, y = feature['geometry']['coordinates'] properties = feature['properties'] supermarket = Supermarket(id_markt, x, y, **properties) supermarkets.append(supermarket) return supermarkets def truncate(self, fc): """ Truncate the table Parameters ---------- fc : str the table to truncate """ arcpy.TruncateTable_management(in_table=fc) if __name__ == '__main__': o = OSMShopsReader() source = Point(54, 10, epsg=4326) #source.transform(3035) supermarkets = o.get_shops() o.create_supermarket_features(supermarkets)