def try_open_geojson(file_path): replaced = False try: gdf = GeoDataFrame.from_file(file_path) except fiona_err.CPLE_AppDefinedError as err: replace_geojson_id_field(file_path) replaced = True gdf = GeoDataFrame.from_file(file_path) return gdf, replaced
def makedata(ifile, zfield, *args): # if csv: if ifile[-4:] == '.csv': data = pd.read_csv(ifile, header=0) # Filter data data = data[(data[zfield] != -9999.0) & (data[lat] != -9999.0) & (pd.isnull(data[lat]) == False)] # Get points and Buffer to create polygon type coords = GeoSeries([Point(x, y) for x, y in zip(data[lon], data[lat])]) data['geometry'] = coords.buffer(0.0001) # Filter out null types data = data[pd.isnull(data.geometry) == False] #Recreate index data.index = [i for i in range(len(data))] return GeoDataFrame(data)# elif ifile[-4:] == '.shp': data = GeoDataFrame.from_file(ifile) # Polygon types if isinstance(data.geometry[1], Polygon) == True: data = data[pd.isnull(data.geometry) == False] return data # Point types elif isinstance(data.geometry[1], Point) == True: coords = data.geometry.buffer(0.0001) data['geometry'] = coords # Filter out null types data = data[pd.isnull(data.geometry) == False] # Recreate index data.index = [i for i in range(len(data))] return GeoDataFrame(data) else: return "Filetype not supported - Only points or polygons!"
def main(): # Read in Data grid = GeoDataFrame.from_file(igrid) points = pd.read_csv(ipoints) # create geopoints geopoints = points2geo(points, lat, lon) # match projection info: ## Points - should already be in wgs84 geopoints.crs =wgs84 geopoints['geometry'] = geopoints['geometry'].to_crs(epsg=4326) ## Grid - project from meters to wgs84 grid.crs = gridproj grid['geometry'] = grid['geometry'].to_crs(epsg=4326) # create uid to groupby grid['id'] = [i for i in range(len(grid))] # Spatial join points to grid join_inner_df = sjoin(grid, geopoints, how="inner") # Group by the uid and geometry - return mean join_inner_df = join_inner_df.groupby(['id','geometry'])['Decibel'].mean() # join_inner_df = join_inner_df.groupby(['id','geometry'])['Decibel'].max() # Create geodataframe & reset the index of the file output = GeoDataFrame(join_inner_df) output = output.reset_index() # output # write to file output.to_file(ofile)
def save_reload(result_layer): """ Save the 'result_layer' geodataframe as a Shapefile and reload it. Save the 'result_layer' geodataframe as a Shapefile (with the hope of taking advantage of the reparation of some geometries offered by one of the intermediate libraries used for saving it), then reload it and return it as a GeoJSON FeatureCollection, loaded in a python 'dict'. Parameters ---------- result_layer: GeoDataFrame The GeoDataFrame containing the contour computed from smoomapy functionnality. Returns ------- result_layer: dict The same layer with, hopefully, some geometry error fixed. The layer is returned loaded as a 'dict', using GeoJSON schema. """ with tempfile.TemporaryDirectory() as tmpdirname: fpath = path_join(tmpdirname, 'result.shp') result_layer.to_file(fpath) gdf = GeoDataFrame.from_file(fpath) return json.loads(gdf[::-1].to_json())
def setUp(self): N = 10 # Data from http://www.nyc.gov/html/dcp/download/bytes/nybb_13a.zip # saved as geopandas/examples/nybb_13a.zip. if not os.path.exists(os.path.join('examples', 'nybb_13a.zip')): with open(os.path.join('examples', 'nybb_13a.zip'), 'w') as f: response = urllib2.urlopen('http://www.nyc.gov/html/dcp/download/bytes/nybb_13a.zip') f.write(response.read()) self.df = GeoDataFrame.from_file( '/nybb_13a/nybb.shp', vfs='zip://examples/nybb_13a.zip') self.tempdir = tempfile.mkdtemp() self.boros = np.array(['Staten Island', 'Queens', 'Brooklyn', 'Manhattan', 'Bronx']) self.crs = {'init': 'epsg:4326'} self.df2 = GeoDataFrame([ {'geometry' : Point(x, y), 'value1': x + y, 'value2': x * y} for x, y in zip(range(N), range(N))], crs=self.crs) # Try to create the database, skip the db tests if something goes # wrong # If you'd like these tests to run, create a database called # 'test_geopandas' and enable postgis in it: # > createdb test_geopandas # > psql -c "CREATE EXTENSION postgis" -d test_geopandas try: self._create_db() self.run_db_test = True except (NameError, OperationalError): # NameError is thrown if psycopg2 fails to import at top of file # OperationalError is thrown if we can't connect to the database self.run_db_test = False
def test_to_file(self): tempfilename = os.path.join(self.tempdir, 'boros.shp') self.df.to_file(tempfilename) # Read layer back in? df = GeoDataFrame.from_file(tempfilename) self.assertTrue('geometry' in df) self.assertTrue(len(df) == 5) self.assertTrue(np.alltrue(df['BoroName'].values == self.boros))
def test_input_with_missing_values(self): gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson") gdf.loc[12:18, "gdppps2008"] = np.NaN idw = SmoothIdw(gdf, "gdppps2008", power=1, nb_pts=2600, mask=gdf) result = idw.render(9, "jenks", output="Geodataframe") self.assertIsInstance(result, GeoDataFrame) self.assertEqual(len(result), 9) gdf2 = GeoDataFrame.from_file('misc/nuts3_data.geojson').to_crs( {"init": "epsg:3035"}) gdf2.loc[:, 'gdppps2008'] = gdf2['gdppps2008'].astype(object) gdf2.loc[15:20, 'gdppps2008'] = "" gdf2.loc[75:78, 'gdppps2008'] = "" idw = SmoothIdw(gdf2, 'gdppps2008', power=1, nb_pts=1200, mask=gdf2) result = idw.render(9, 'jenks', output="GeoDataFrame") self.assertIsInstance(result, GeoDataFrame) self.assertEqual(len(result), 9)
def test_to_file_int64(tmpdir, df_points): tempfilename = os.path.join(str(tmpdir), "int64.shp") geometry = df_points.geometry df = GeoDataFrame(geometry=geometry) df["data"] = pd.array([1, np.nan] * 5, dtype=pd.Int64Dtype()) df.to_file(tempfilename) df_read = GeoDataFrame.from_file(tempfilename) assert_geodataframe_equal(df_read, df, check_dtype=False, check_like=True)
def get_latitude(scenario_path): import cea.inputlocator data = gdf.from_file( cea.inputlocator.InputLocator(scenario_path).get_zone_geometry()) data = data.to_crs(get_geographic_coordinate_system()) latitude = data.geometry[0].centroid.coords.xy[1][0] return latitude
def joinTrafficCounts(data_grid, utm10n): data_grid['gid'] = data_grid.id data_grid.crs = utm10n; print data_grid.crs osm_trafficCounts_centroids = GeoDataFrame.from_file(os.getcwd()+'/diysco2-db/_main_/yvr-open-data-traffic-counts/generated-traffic-counts-osm-split/'+'osm_trafficCounts_split_dev_'+str(50)+'_centroids.shp') gridded_traffic_counts = sjoin(data_grid, osm_trafficCounts_centroids, how="left") print len(gridded_traffic_counts) return gridded_traffic_counts
def convert(name: str): shpName = join(source_path,name, name) + '.shp' jsonName = join(output_path, name) + '.json' if not isfile(jsonName): gdf = GeoDataFrame.from_file(shpName, encoding='tis-620') gdf = gdf.to_crs(epsg=4326) gdf.to_file(jsonName, driver="GeoJSON") formatJsonFile(jsonName)
def schedule_maker_main(locator, config, building=None): # local variables buildings = config.schedule_maker.buildings schedule_model = config.schedule_maker.schedule_model if schedule_model == 'deterministic': stochastic_schedule = False elif schedule_model == 'stochastic': stochastic_schedule = True else: raise ValueError("Invalid schedule model: {schedule_model}".format(**locals())) if building != None: buildings = [building] # this is to run the tests # CHECK DATABASE if is_3_22(config.scenario): raise ValueError("""The data format of indoor comfort has been changed after v3.22. Please run Data migrator in Utilities.""") # get variables of indoor comfort and internal loads internal_loads = dbf_to_dataframe(locator.get_building_internal()).set_index('Name') indoor_comfort = dbf_to_dataframe(locator.get_building_comfort()).set_index('Name') architecture = dbf_to_dataframe(locator.get_building_architecture()).set_index('Name') # get building properties prop_geometry = Gdf.from_file(locator.get_zone_geometry()) prop_geometry['footprint'] = prop_geometry.area prop_geometry['GFA_m2'] = prop_geometry['footprint'] * (prop_geometry['floors_ag'] + prop_geometry['floors_bg']) prop_geometry['GFA_ag_m2'] = prop_geometry['footprint'] * prop_geometry['floors_ag'] prop_geometry['GFA_bg_m2'] = prop_geometry['footprint'] * prop_geometry['floors_bg'] prop_geometry = prop_geometry.merge(architecture, on='Name').set_index('Name') prop_geometry = calc_useful_areas(prop_geometry) # get calculation year from weather file weather_path = locator.get_weather_file() weather_data = epwreader.epw_reader(weather_path)[['year', 'drybulb_C', 'wetbulb_C', 'relhum_percent', 'windspd_ms', 'skytemp_C']] year = weather_data['year'][0] # create date range for the calculation year date_range = get_date_range_hours_from_year(year) # SCHEDULE MAKER n = len(buildings) calc_schedules_multiprocessing = cea.utilities.parallel.vectorize(calc_schedules, config.get_number_of_processes(), on_complete=print_progress) calc_schedules_multiprocessing(repeat(locator, n), buildings, repeat(date_range, n), [internal_loads.loc[b] for b in buildings], [indoor_comfort.loc[b] for b in buildings], [prop_geometry.loc[b] for b in buildings], repeat(stochastic_schedule, n)) return None
def test_to_file(self): with tempfile.NamedTemporaryFile(suffix='.shp') as t: self.df.to_file(t.name) # Read layer back in? df = GeoDataFrame.from_file(t.name) self.assertTrue('geometry' in df) self.assertTrue(len(df) == 5) self.assertTrue(np.alltrue(df['BoroName'].values == np.array(['Staten Island', 'Queens', 'Brooklyn', 'Manhattan', 'Bronx'])))
def test_mini_raster(): from geopandas import GeoDataFrame polygons = os.path.join(DATA, 'polygons.shp') df = GeoDataFrame.from_file(polygons) stats = zonal_stats(df.geometry, raster, raster_out=True) stats2 = zonal_stats(df.geometry, stats[0]['mini_raster'], raster_out=True, transform=stats[0]['mini_raster_GT']) assert (stats[0]['mini_raster'] == stats2[0]['mini_raster']).sum() == \ stats[0]['count']
def get_geo_dataframe(self): self._model.geo_dataframe = GeoDataFrame.from_file( self._model.file_path) # init coordination if self._model.coordination in CONST.EPSG: self._model.geo_dataframe.crs = { 'init': 'epsg:' + str(CONST.EPSG[self._model.coordination]) } print(self._model.geo_dataframe.head())
def test_to_file(tmpdir, df_nybb, df_null, driver, ext): """ Test to_file and from_file """ tempfilename = os.path.join(str(tmpdir), "boros." + ext) df_nybb.to_file(tempfilename, driver=driver) # Read layer back in df = GeoDataFrame.from_file(tempfilename) assert "geometry" in df assert len(df) == 5 assert np.alltrue(df["BoroName"].values == df_nybb["BoroName"]) # Write layer with null geometry out to file tempfilename = os.path.join(str(tmpdir), "null_geom." + ext) df_null.to_file(tempfilename, driver=driver) # Read layer back in df = GeoDataFrame.from_file(tempfilename) assert "geometry" in df assert len(df) == 2 assert np.alltrue(df["Name"].values == df_null["Name"])
def calc_area_buildings(locator, buildings_list): # initialize value prop_geometry = Gdf.from_file(locator.get_zone_geometry()) prop_geometry['footprint'] = prop_geometry.area footprint = prop_geometry[prop_geometry["Name"].isin(buildings_list)]['footprint'] area_below_buildings = footprint.sum() return area_below_buildings
def test_to_file_pathlib(tmpdir, df_nybb, df_null, driver, ext): """ Test to_file and from_file """ temppath = pathlib.Path(os.path.join(str(tmpdir), "boros." + ext)) df_nybb.to_file(temppath, driver=driver) # Read layer back in df = GeoDataFrame.from_file(temppath) assert "geometry" in df assert len(df) == 5 assert np.alltrue(df["BoroName"].values == df_nybb["BoroName"])
def test_to_file(self): """ Test to_file and from_file """ tempfilename = os.path.join(self.tempdir, 'boros.shp') self.df.to_file(tempfilename) # Read layer back in df = GeoDataFrame.from_file(tempfilename) assert 'geometry' in df assert len(df) == 5 assert np.alltrue(df['BoroName'].values == self.boros) # Write layer with null geometry out to file tempfilename = os.path.join(self.tempdir, 'null_geom.shp') self.df3.to_file(tempfilename) # Read layer back in df3 = GeoDataFrame.from_file(tempfilename) assert 'geometry' in df3 assert len(df3) == 2 assert np.alltrue(df3['Name'].values == self.line_paths)
def sampling_single(locator, random_variables, target_parameters, list_building_names, config, nn_delay, climatic_variables, year, use_stochastic_occupancy): size_city = np.shape(list_building_names) size_city = size_city[0] bld_counter = 0 # create list of samples with a LHC sampler and save to disk (*.csv) samples, samples_norm, pdf_list = latin_sampler(locator, size_city, random_variables) for building_name in (list_building_names): np.save(locator.get_calibration_folder(), samples) building_load = config.single_calibration.load override_file = Gdf.from_file( locator.get_zone_geometry()).set_index('Name') override_file = pd.DataFrame(index=override_file.index) problem = { 'variables': random_variables, 'building_load': target_parameters, 'probabiltiy_vars': pdf_list } pickle.dump( problem, file(locator.get_calibration_problem(building_name, building_load), 'w')) sample = np.asarray(zip(random_variables, samples[bld_counter, :])) apply_sample_parameters(locator, sample, override_file) bld_counter = bld_counter + 1 # read the saved *.csv file and replace Boolean with logical (True/False) overwritten = pd.read_csv(locator.get_building_overrides()) bld_counter = 0 for building_name in (list_building_names): sample = np.asarray(zip(random_variables, samples[bld_counter, :])) for boolean_mask in (boolean_vars): indices = np.where(sample == boolean_mask) if sample[indices[0], 1] == '0.0': sample[indices[0], 1] = 'False' else: sample[indices[0], 1] = 'True' overwritten.loc[overwritten.Name == building_name, random_variables] = sample[:, 1] bld_counter = bld_counter + 1 # write to csv format overwritten.to_csv(locator.get_building_overrides()) # run cea demand demand_main.demand_calculation(locator, config) # prepare the inputs for feeding into the neural network urban_input_matrix, urban_taget_matrix = input_prepare_main( list_building_names, locator, target_parameters, nn_delay, climatic_variables, year, use_stochastic_occupancy) return urban_input_matrix, urban_taget_matrix
def test_to_file(self): """ Test to_file and from_file """ tempfilename = os.path.join(self.tempdir, "boros.shp") self.df.to_file(tempfilename) # Read layer back in df = GeoDataFrame.from_file(tempfilename) self.assertTrue("geometry" in df) self.assertTrue(len(df) == 5) self.assertTrue(np.alltrue(df["BoroName"].values == self.boros)) # Write layer with null geometry out to file tempfilename = os.path.join(self.tempdir, "null_geom.shp") self.df3.to_file(tempfilename) # Read layer back in df3 = GeoDataFrame.from_file(tempfilename) self.assertTrue("geometry" in df3) self.assertTrue(len(df3) == 2) self.assertTrue(np.alltrue(df3["Name"].values == self.line_paths))
def surroundings_building_records(self): surroundings_buildings_df = gdf.from_file( self.locator.get_surroundings_geometry()) # clear in case there are repetitive buildings in the zone file surroundings_buildings_df = surroundings_buildings_df.loc[ ~surroundings_buildings_df["Name"].isin(self.zone_building_names)] surroundings_buildings_df.reset_index(inplace=True, drop=True) return surroundings_buildings_df
def test_geometries(tmpdir): point_file = '{}/test_file_point.shp'.format(tmpdir) polygon_file = '{}/test_file_polygon.shp'.format(tmpdir) polyline_file = '{}/test_file_polyline.shp'.format(tmpdir) _point_file(point_file) _polygon_file(polygon_file) _polyline_file(polyline_file) df_point = GeoDataFrame.from_file(point_file) df_polygon = GeoDataFrame.from_file(polygon_file) df_polyline = GeoDataFrame.from_file(polyline_file) p = (ggplot() + aes(fill='geometry.bounds.miny') + geom_map(df_polygon) + geom_map(df_point, size=4) + geom_map(df_polyline, size=2) + labs(fill='miny')) assert p + _theme == 'geometries'
def building_2d_to_3d(locator, zone_df, surroundings_df, elevation_map, config, geometry_pickle_dir): """ :param locator: InputLocator - provides paths to files in a scenario :type locator: cea.inputlocator.InputLocator :param config: the configuration object to use :type config: cea.config.Configuration :return: """ # Config variables num_processes = config.get_number_of_processes() zone_simplification = config.radiation.zone_geometry surroundings_simplification = config.radiation.surrounding_geometry consider_intersections = config.radiation.consider_intersections print('Calculating terrain intersection of building geometries') zone_buildings_df = zone_df.set_index('Name') zone_building_names = zone_buildings_df.index.values zone_building_solid_list = calc_building_solids(zone_buildings_df, zone_simplification, elevation_map, num_processes) surroundings_buildings_df = surroundings_df.set_index('Name') surroundings_building_names = surroundings_buildings_df.index.values surroundings_building_solid_list = calc_building_solids( surroundings_buildings_df, surroundings_simplification, elevation_map, num_processes) architecture_wwr_df = gdf.from_file( locator.get_building_architecture()).set_index('Name') # calculate geometry for the surroundings print('Generating geometry for surrounding buildings') geometry_3D_surroundings = [ calc_building_geometry_surroundings(x, y, geometry_pickle_dir) for x, y in zip(surroundings_building_names, surroundings_building_solid_list) ] # calculate geometry for the zone of analysis print('Generating geometry for buildings in the zone of analysis') n = len(zone_building_names) calc_zone_geometry_multiprocessing = cea.utilities.parallel.vectorize( calc_building_geometry_zone, num_processes, on_complete=print_progress) if consider_intersections: all_building_solid_list = np.append(zone_building_solid_list, surroundings_building_solid_list) else: all_building_solid_list = [] geometry_3D_zone = calc_zone_geometry_multiprocessing( zone_building_names, zone_building_solid_list, repeat(all_building_solid_list, n), repeat(architecture_wwr_df, n), repeat(geometry_pickle_dir, n), repeat(consider_intersections, n)) return geometry_3D_zone, geometry_3D_surroundings
def main(): # read in data and fix headers data = pd.read_csv('/Users/Jozo/Dropbox/UBC/cirs/energyexplorer/data/optimized/energy/consumption/ceei_2010_metrovan.csv') data.columns = fixheaders(data) # electricity (mostly renewable), heating fuel (fossil fuels), & transporation (fossil fuels) by LocalGovtName groupedData =data.groupby(['LocalGovtName', 'LocalGovtType', 'MeasurementDesc', 'Sector'])['EnergyGJ'].sum() groupedData.to_csv('/Users/Jozo/Dropbox/UBC/cirs/energyexplorer/data/optimized/energy/consumption/ceei_2010_metrovan_grouped.csv') # read in grouped data newdata = pd.read_csv('/Users/Jozo/Dropbox/UBC/cirs/energyexplorer/data/optimized/energy/consumption/ceei_2010_metrovan_grouped.csv', header=False) newdata.columns = ['city','ctype', 'sector', 'desc', 'consump'] # Add in metro vancouver names: newdata['metroname'] = pd.Series() for i in np.arange(0,len(newdata), 1): if newdata.ctype[i] == 'City' or newdata.ctype[i] == 'Village': newdata.metroname[i] = str(newdata.ctype[i] + " of " + newdata.city[i]) elif newdata.city[i] == 'Delta' or newdata.city[i] == 'Langley' and newdata.ctype[i] == 'District Municipality': newdata.metroname[i] = str('Township' + " of " + newdata.city[i]) elif newdata.city[i] == 'Maple Ridge' or newdata.city[i] == 'North Vancouver' or newdata.city[i] == 'West Vancouver' and newdata.ctype[i] == 'District Municipality': newdata.metroname[i] = str('District' + " of " + newdata.city[i]) elif newdata.city[i] == 'Bowen Island' and newdata.ctype[i] == 'Island Municipality': newdata.metroname[i] = str('Village' + " of " + newdata.city[i]) elif newdata.city[i] == 'Metro-Vancouver' and newdata.ctype[i] == 'Regional District Unincorporated Areas': newdata.metroname[i] = str('Electoral Area A') elif newdata.city[i] == 'Metro-Vancouver' and newdata.ctype[i] == 'Regional District': newdata.metroname[i] = str(newdata.city[i] + " " + newdata.ctype[i]) newdata.to_csv('/Users/Jozo/Dropbox/UBC/cirs/energyexplorer/data/optimized/energy/consumption/ceei_2010_metrovan_grouped_metronames.csv') # Pivot data newdata_pivot =newdata.pivot(index = 'metroname',columns ='sector', values='consump') #fill na with 0 newdata_pivot = newdata_pivot.fillna(0) # fix columns newdata_pivot.columns = fixheaders(newdata_pivot) newdata_pivot.to_csv('/Users/Jozo/Dropbox/UBC/cirs/energyexplorer/data/optimized/energy/consumption/ceei_2010_metrovan_grouped_metronames_formatted.csv', index='metroname') ''' --- merge with shapefile --- ''' metrovan = GeoDataFrame.from_file('/Users/Jozo/Dropbox/UBC/CIRs/EnergyExplorer/data/optimized/metroVan/metroVan.shp') joindata = pd.read_csv('/Users/Jozo/Dropbox/UBC/cirs/energyexplorer/data/optimized/energy/consumption/ceei_2010_metrovan_grouped_metronames_formatted.csv', header=False) output = GeoDataFrame.merge(metrovan, joindata, left_on="NAMEMUNI", right_on="metroname") outputcol = [i for i in joindata.columns] outputcol.append('geometry') output = output[outputcol] output.to_file('/Users/Jozo/Dropbox/UBC/CIRs/EnergyExplorer/data/optimized/energy/consumption/ceei_2010/ceei_2010_metrovan_formatted.shp')
def testing(): from geopandas import GeoDataFrame as gpdf import cea.globalvar gv = cea.globalvar.GlobalVariables() from cea import inputlocator import time import simple_window_generator # generate windows based on geometry of vertical surfaces in radiation file locator = inputlocator.InputLocator(scenario_path=r'C:\reference-case\baseline') surface_properties = pd.read_csv(locator.get_surface_properties()) gdf_building_architecture = gpdf.from_file(locator.get_building_architecture()).drop('geometry', axis=1).set_index('Name') prop_geometry = gpdf.from_file(locator.get_building_geometry()) prop_geometry['footprint'] = prop_geometry.area prop_geometry['perimeter'] = prop_geometry.length prop_geometry = prop_geometry.drop('geometry', axis=1).set_index('Name') df_windows = simple_window_generator.create_windows(surface_properties, gdf_building_architecture) building_test = 'B153737' # 'B154767' this building doesn't have windows # get building windows df_windows_building_test = df_windows.loc[df_windows['name_building'] == building_test].to_dict('list') # get building geometry gdf_building_test = prop_geometry.ix[building_test] gdf_building_architecture = gdf_building_architecture.ix[building_test] r_window_arg = 0.1 temp_ext = 5 temp_zone = 22 u_wind = 0.5 u_wind_10 = u_wind factor_cros = 1 # 1 = cross ventilation possible # TODO: get from building properties dict_props_nat_vent = get_properties_natural_ventilation(gdf_building_test, gdf_building_architecture, gv) qm_arg_in, qm_arg_out \ = calc_qm_arg(factor_cros, temp_ext, df_windows_building_test, u_wind_10, temp_zone, r_window_arg) t0 = time.time() res = calc_air_flows(temp_zone, u_wind, temp_ext, dict_props_nat_vent) t1 = time.time() print(res) print(['time: ', t1 - t0])
def test_input_with_missing_values(self): gdf = GeoDataFrame.from_file("misc/nuts3_data.geojson") gdf.loc[12:18, "gdppps2008"] = np.NaN StePot = SmoothStewart(gdf, "gdppps2008", span=65000, beta=2, resolution=100000, mask=gdf) result = StePot.render(9, "equal_interval", output="Geodataframe") self.assertIsInstance(result, GeoDataFrame) self.assertEqual(len(result), 9) gdf2 = GeoDataFrame.from_file('misc/nuts3_data.geojson').to_crs({"init": "epsg:3035"}) gdf2.loc[:, 'gdppps2008'] = gdf2['gdppps2008'].astype(object) gdf2.loc[15:20, 'gdppps2008'] = "" gdf2.loc[75:78, 'gdppps2008'] = "" StePot = SmoothStewart(gdf2, 'gdppps2008', span=65000, beta=2, resolution=80000, mask=gdf2) result = StePot.render(9, 'equal_interval', output="GeoDataFrame") self.assertIsInstance(result, GeoDataFrame) self.assertEqual(len(result), 9)
def test_to_file_with_poly_z(self): """Test that 3D geometries are retained in writes (GH #612).""" tempfilename = os.path.join(self.tempdir, 'test_3Dpoly.shp') poly3d = Polygon([[0, 0, 5], [0, 1, 5], [1, 1, 5], [1, 0, 5]]) poly2d = Polygon([[0, 0], [0, 1], [1, 1], [1, 0]]) df = GeoDataFrame({'a': [1, 2]}, geometry=[poly3d, poly2d], crs={}) df.to_file(tempfilename) df_read = GeoDataFrame.from_file(tempfilename) assert_geoseries_equal(df.geometry, df_read.geometry)
def test_to_file_with_point_z(self): """Test that 3D geometries are retained in writes (GH #612).""" tempfilename = os.path.join(self.tempdir, 'test_3Dpoint.shp') point3d = Point(0, 0, 500) point2d = Point(1, 1) df = GeoDataFrame({'a': [1, 2]}, geometry=[point3d, point2d], crs={}) df.to_file(tempfilename) df_read = GeoDataFrame.from_file(tempfilename) assert_geoseries_equal(df.geometry, df_read.geometry)
def test_facet_wrap(tmpdir): polygon_file = '{}/test_file_polygon.shp'.format(tmpdir) _polygon_file(polygon_file) df_polygon = GeoDataFrame.from_file(polygon_file) df_polygon['shape'] = ['rectangle', 'triangle'] p = (ggplot() + aes(fill='geometry.bounds.miny') + geom_map(df_polygon) + facet_wrap('shape') + labs(fill='miny')) assert p + _theme == 'facet_wrap'
def test_to_file_with_poly_z(tmpdir, ext, driver): """Test that 3D geometries are retained in writes (GH #612).""" tempfilename = os.path.join(str(tmpdir), "test_3Dpoly." + ext) poly3d = Polygon([[0, 0, 5], [0, 1, 5], [1, 1, 5], [1, 0, 5]]) poly2d = Polygon([[0, 0], [0, 1], [1, 1], [1, 0]]) df = GeoDataFrame({"a": [1, 2]}, geometry=[poly3d, poly2d], crs=_CRS) df.to_file(tempfilename, driver=driver) df_read = GeoDataFrame.from_file(tempfilename) assert_geoseries_equal(df.geometry, df_read.geometry)
def test_geometries(tmpdir): test_file = '{}/test_file.shp'.format(tmpdir) _create_test_input_files(test_file) df = GeoDataFrame.from_file(test_file) p = (ggplot(df) + aes(fill='geometry.bounds.miny') + geom_map() + geom_map(draw='Point', size=4) + geom_map(draw='LineString', size=2) + labs(fill='miny')) assert p + _theme == 'geometries'
def test_featurecollection(): from geopandas import GeoDataFrame polygons = os.path.join(DATA, 'polygons.shp') df = GeoDataFrame.from_file(polygons) assert df.__geo_interface__['type'] == 'FeatureCollection' stats = zonal_stats(polygons, raster) # geointerface featurecollection stats2 = zonal_stats(df, raster) assert stats == stats2
def test_add_stats(): from geopandas import GeoDataFrame polygons = os.path.join(DATA, 'polygons.shp') df = GeoDataFrame.from_file(polygons) def mymean(x): return np.ma.mean(x) stats = zonal_stats(df.geometry, raster, add_stats={'mymean': mymean}) for i in range(len(stats)): assert stats[i]['mean'] == stats[i]['mymean']
def read_vector(vector, res, column=None, value=1., compute_area=False, dtype=np.float64, eea=False, epsg=None, bounds=None, grid=None, all_touched=True, fillvalue=0., use_centroid=False): logger.debug('Reading vector as geodataframe') gdf = GeoDataFrame.from_file(vector) if use_centroid: gdf.geometry = gdf.geometry.centroid return read_df(gdf, res, column, value, compute_area, dtype, eea, epsg, bounds, grid, all_touched=all_touched, fillvalue=fillvalue)
def main(config): assert os.path.exists( config.scenario), 'Scenario not found: %s' % config.scenario locator = cea.inputlocator.InputLocator(scenario=config.scenario) print('Running photovoltaic with scenario = %s' % config.scenario) print('Running photovoltaic with annual-radiation-threshold-kWh/m2 = %s' % config.solar.annual_radiation_threshold) print('Running photovoltaic with panel-on-roof = %s' % config.solar.panel_on_roof) print('Running photovoltaic with panel-on-wall = %s' % config.solar.panel_on_wall) print('Running photovoltaic with solar-window-solstice = %s' % config.solar.solar_window_solstice) print('Running photovoltaic with type-pvpanel = %s' % config.solar.type_pvpanel) if config.solar.custom_tilt_angle: print( 'Running photovoltaic with custom-tilt-angle = %s and panel-tilt-angle = %s' % (config.solar.custom_tilt_angle, config.solar.panel_tilt_angle)) else: print('Running photovoltaic with custom-tilt-angle = %s' % config.solar.custom_tilt_angle) if config.solar.custom_roof_coverage: print( 'Running photovoltaic with custom-roof-coverage = %s and max-roof-coverage = %s' % (config.solar.custom_roof_coverage, config.solar.max_roof_coverage)) else: print('Running photovoltaic with custom-roof-coverage = %s' % config.solar.custom_roof_coverage) building_names = locator.get_zone_building_names() zone_geometry_df = gdf.from_file(locator.get_zone_geometry()) latitude, longitude = get_lat_lon_projected_shapefile(zone_geometry_df) # list_buildings_names =['B026', 'B036', 'B039', 'B043', 'B050'] for missing buildings weather_data = epwreader.epw_reader(locator.get_weather_file()) date_local = solar_equations.calc_datetime_local_from_weather_file( weather_data, latitude, longitude) num_process = config.get_number_of_processes() n = len(building_names) cea.utilities.parallel.vectorize(calc_PV, num_process)(repeat(locator, n), repeat(config, n), repeat(latitude, n), repeat(longitude, n), repeat(weather_data, n), repeat(date_local, n), building_names) # aggregate results from all buildings write_aggregate_results(locator, building_names, num_process)
def open_mask(self, mask, input_layer): # Read the mask according to its format: if isinstance(mask, GeoDataFrame): self.mask = mask elif isinstance(mask, str) and isinstance(input_layer, str) \ and mask == input_layer: self.mask = self.gdf.copy() else: self.mask = GeoDataFrame.from_file(mask) self.check_mask()
def calc_building_centroids(input_buildings_shp, temp_path_building_centroids_shp, list_district_scale_buildings, plant_buildings, consider_only_buildings_with_demand=False, type_network="DH", total_demand=False): # # get coordinate system and project to WSG 84 zone_df = gdf.from_file(input_buildings_shp) zone_df = zone_df.loc[zone_df['Name'].isin(list_district_scale_buildings + plant_buildings)] zone_df = zone_df.reset_index(drop=True) # get only buildings with a demand, send out a message if there are less than 2 buildings. if consider_only_buildings_with_demand: total_demand = pd.read_csv(total_demand) if type_network == "DH": field = "QH_sys_MWhyr" elif type_network == "DC": field = "QC_sys_MWhyr" buildings_with_load = total_demand[ total_demand[field] > 0.0].Name.tolist() selected_buildings = list( set(buildings_with_load).union(set(plant_buildings))) if len(selected_buildings) >= 2: zone_df = zone_df.loc[zone_df['Name'].isin(selected_buildings)] zone_df = zone_df.reset_index(drop=True) else: raise Exception( "We could not find two or more buildings with thermal energy demand the network layout " "will not work unless the consider_only_buildings_with_demand parameter is set to False" ) zone_df = zone_df.to_crs(get_geographic_coordinate_system()) lon = zone_df.geometry[0].centroid.coords.xy[0][0] lat = zone_df.geometry[0].centroid.coords.xy[1][0] # get coordinate system and re project to UTM zone_df = zone_df.to_crs(get_projected_coordinate_system(lat, lon)) # create points with centroid points = zone_df.copy() points.geometry = zone_df['geometry'].centroid # # decrease the number of units of the points building_centroids_df = simplify_points_accurracy(points, SHAPEFILE_TOLERANCE, points.crs) # saving result building_centroids_df.to_file(temp_path_building_centroids_shp, driver='ESRI Shapefile') return building_centroids_df
def test_to_file_with_point_z(tmpdir, ext, driver): """Test that 3D geometries are retained in writes (GH #612).""" tempfilename = os.path.join(str(tmpdir), 'test_3Dpoint.' + ext) point3d = Point(0, 0, 500) point2d = Point(1, 1) df = GeoDataFrame({'a': [1, 2]}, geometry=[point3d, point2d], crs={'init': 'epsg:4326'}) df.to_file(tempfilename, driver=driver) df_read = GeoDataFrame.from_file(tempfilename) assert_geoseries_equal(df.geometry, df_read.geometry)
def test_to_file_with_point_z(tmpdir, ext, driver): """Test that 3D geometries are retained in writes (GH #612).""" tempfilename = os.path.join(str(tmpdir), "test_3Dpoint" + ext) point3d = Point(0, 0, 500) point2d = Point(1, 1) df = GeoDataFrame({"a": [1, 2]}, geometry=[point3d, point2d], crs=_CRS) df.to_file(tempfilename, driver=driver) df_read = GeoDataFrame.from_file(tempfilename) assert_geoseries_equal(df.geometry, df_read.geometry) # check the expected driver assert_correct_driver(tempfilename, ext)
def setUp(self): # Data from http://www.nyc.gov/html/dcp/download/bytes/nybb_13a.zip # saved as geopandas/examples/nybb_13a.zip. if not os.path.exists(os.path.join('examples', 'nybb_13a.zip')): with open(os.path.join('examples', 'nybb_13a.zip'), 'w') as f: response = urllib2.urlopen('http://www.nyc.gov/html/dcp/download/bytes/nybb_13a.zip') f.write(response.read()) self.df = GeoDataFrame.from_file( '/nybb_13a/nybb.shp', vfs='zip://examples/nybb_13a.zip') self.tempdir = tempfile.mkdtemp() self.boros = np.array(['Staten Island', 'Queens', 'Brooklyn', 'Manhattan', 'Bronx'])
def __init__(self, locator, settings, geometry_terrain, height_col, nfloor_col): self.point_to_evaluate = '' self.potentially_intersecting_solids = '' self.locator = locator self.height_col = height_col self.nfloor_col = nfloor_col self.settings = settings self.architecture_wwr_df = gdf.from_file(self.locator.get_building_architecture()).set_index('Name') self.terrain_intersection_curves = self.terrain_intersection_curves(geometry_terrain) self.zone_buildings_df = gdf.from_file(self.locator.get_zone_geometry()).set_index('Name') self.zone_building_names = self.zone_buildings_df.index.values self.zone_building_solid_list = np.vectorize(self.calc_zone_building_solids)(self.zone_building_names) self.surroundings_buildings_df = self.surroundings_building_records().set_index('Name') self.surroundings_building_names = self.surroundings_buildings_df.index.values self.surroundings_building_solid_list = np.vectorize(self.calc_surrounding_building_solids, otypes=[object])(self.surroundings_building_names) self.all_building_solid_list = np.append(self.zone_building_solid_list, self.surroundings_building_solid_list)
def test_geometries(tmpdir): test_file = '{}/test_file.shp'.format(tmpdir) _create_test_input_files(test_file) df = GeoDataFrame.from_file(test_file) p = (ggplot(df) + aes(fill='geometry.bounds.miny') + geom_map() + geom_map(draw='Point', size=4) + geom_map(draw='LineString', size=2) + labs(fill='miny') ) assert p + _theme == 'geometries'
def lca_mobility(locator): """ algorithm to calculate the primary energy and CO2 emissions for mobility in the area in order to compare with the 2000 Watt society benchmark based on SIA Standard 2039 (expanded to include industry and hospital buildings) Parameters ---------- :param InputLocator locator: an InputLocator instance set to the scenario to work on Returns ------- total_LCA_mobility:.csv csv file of yearly primary energy demand and emissions due to mobility for each building """ # local files demand = pd.read_csv(locator.get_total_demand()) prop_occupancy = gpdf.from_file(locator.get_building_occupancy()).drop('geometry', axis=1)#.set_index('Name') data_mobility = locator.get_data_mobility() factors_mobility = pd.read_excel(data_mobility, sheetname='2010') # local variables result_folder = locator.get_lca_emissions_results_folder() # calculate total_LCA_mobility:.csv vt = factors_mobility['code'] pt = factors_mobility['PEN'] gt = factors_mobility['CO2'] mobility = prop_occupancy.merge(demand,on='Name') fields_to_plot = ['Name', 'GFA_m2', 'M_nre_pen_GJ', 'M_nre_pen_MJm2', 'M_ghg_ton', 'M_ghg_kgm2'] mobility[fields_to_plot[3]] = 0 mobility[fields_to_plot[5]] = 0 for i in range(len(vt)): mobility[fields_to_plot[3]] += mobility[vt[i]] * pt[i] mobility[fields_to_plot[5]] += mobility[vt[i]] * gt[i] mobility[fields_to_plot[2]] = mobility['GFA_m2'] * mobility[fields_to_plot[3]] / 1000 mobility[fields_to_plot[4]] = mobility['GFA_m2'] * mobility[fields_to_plot[5]] / 1000 mobility[fields_to_plot].to_csv(locator.get_lca_mobility(), index=False, float_format='%.2f')
def calc_benchmark_today(locator): ''' Calculates the embodied, operation, mobility and total targets (ghg_kgm2 and pen_MJm2) for the area for the current national trend. CURRENTLY BASED ON INDUCITY! Need a better source. :param locator: an InputLocator set to the scenario to compute :array values_today: pen_MJm2 and ghg_kgm2 for the scenario based on benchmarked present day situation ''' # local files demand = pd.read_csv(locator.get_total_demand()) prop_occupancy = gpdf.from_file(locator.get_building_occupancy()).drop('geometry', axis=1) data_benchmark_today = locator.get_data_benchmark_today() occupancy = prop_occupancy.merge(demand, on='Name') fields = ['Name', 'pen_GJ', 'ghg_ton', 'pen_MJm2', 'ghg_kgm2'] categories = ['embodied', 'operation', 'mobility', 'total'] suffix = ['_GJ', '_ton', '_MJm2', '_kgm2'] values_today = {} area_study = 0 factors = pd.read_excel(data_benchmark_today, sheetname=categories[0]) for i in range(len(factors['code'])): if factors['code'][i] in occupancy: if factors['PEN'][i] > 0 and factors['CO2'][i] > 0: area_study += (occupancy['GFA_m2'] * occupancy[factors['code'][i]]).sum() for category in categories: factors = pd.read_excel(data_benchmark_today, sheetname=category) vt = factors['code'] pt = factors['PEN'] gt = factors['CO2'] for j in range(len(suffix)): values_today[category + suffix[j]] = 0 for i in range(len(vt)): values_today[category + suffix[0]] += (occupancy['GFA_m2'] * occupancy[vt[i]] * pt[i]).sum() / 1000 values_today[category + suffix[1]] += (occupancy['GFA_m2'] * occupancy[vt[i]] * gt[i]).sum() / 1000 values_today[category + suffix[2]] += values_today[category + suffix[0]] / area_study * 1000 values_today[category + suffix[3]] += values_today[category + suffix[1]] / area_study * 1000 return values_today
def calc_benchmark_targets(locator): ''' Calculates the embodied, operation, mobility and total targets (ghg_kgm2 and pen_MJm2) for all buildings in a scenario. :param locator: an InputLocator set to the scenario to compute :array target: pen_MJm2 and ghg_kgm2 target values ''' # local files demand = pd.read_csv(locator.get_total_demand()) prop_occupancy = gpdf.from_file(locator.get_building_occupancy()).drop('geometry', axis=1) data_benchmark = locator.get_data_benchmark() occupancy = prop_occupancy.merge(demand,on='Name') fields = ['Name', 'pen_GJ', 'ghg_ton', 'pen_MJm2', 'ghg_kgm2'] categories = ['embodied', 'operation', 'mobility', 'total'] suffix = ['_GJ', '_ton','_MJm2', '_kgm2'] targets = {} area_study = 0 factors = pd.read_excel(data_benchmark, sheetname=categories[0]) for i in range(len(factors['code'])): if factors['code'][i] in occupancy: if factors['PEN'][i] > 0 and factors['CO2'][i] > 0: area_study += (occupancy['GFA_m2'] * occupancy[factors['code'][i]]).sum() for category in categories: factors = pd.read_excel(data_benchmark, sheetname = category) vt = factors['code'] pt = factors['PEN'] gt = factors['CO2'] for j in range(len(suffix)): targets[category + suffix[j]] = 0 for i in range(len(vt)): targets[category+suffix[0]] += (occupancy['GFA_m2'] * occupancy[vt[i]] * pt[i]).sum() / 1000 targets[category+suffix[1]] += (occupancy['GFA_m2'] * occupancy[vt[i]] * gt[i]).sum() / 1000 targets[category + suffix[2]] += targets[category+suffix[0]] / area_study * 1000 targets[category + suffix[3]] += targets[category + suffix[1]] / area_study * 1000 return targets
def load_data(self, file_path=None): """ Loads the environmental data from the provided :attr:`file_path` shapefile into a ``geopandas.GeoDataFrame``. A GeoDataFrame is a tablular data structure that contains a column called ``geometry`` which contains a GeoSeries of `Shapely <http://toblerity.org/shapely/shapely.geometry.html>`_ geometries. all other meta-data column names are converted to a lower-case, for consistency. :param string file_path: The full path to the shapefile file (including the directory and filename in one string). :returns: None """ if file_path: self.file_path = file_path if not self.file_path: raise AttributeError("Please provide a file_path argument to load the data from.") logger.info("Loading data from %s " % self.file_path) self.data_full = GeoDataFrame.from_file(self.file_path) self.data_full.columns = [x.lower() for x in self.data_full.columns] logger.info("The shapefile contains data on %d environmental regions." % self.data_full.shape[0])
def import_census_data(self): """ Gets population density from census data Inputs: census_folder_loc: location of data (string) census_shapefile: name of shapefile (string) Returns: df_census: GeoDataFrame with census information """ ## Importing shapefile print 'Importing census data' self.df_census = GeoDataFrame.from_file( self.census_folder_loc + self.census_shapefile) print 'Census data loaded' # It turns out the earth isn't flat # Getting area in km**2 print 'Calculating area' area_sq_degrees = self.df_census['geometry'] area_sq_km = [] for region in area_sq_degrees: geom_area = ops.transform( partial( pyproj.transform, # projection GSCNAD83 # southern WA EPSG:2286 pyproj.Proj(init='EPSG:4326'), pyproj.Proj( proj='aea', lat1=region.bounds[1], lat2=region.bounds[3] ) ), region) area = geom_area.area / 1000000.0 #convert m2 to km2 area_sq_km.append( area ) self.df_census['area'] = area_sq_km self.df_census['density'] = \ self.df_census['POP10'] / self.df_census['area'] print 'Area calculated'
def from_file(cls, filename, **kwargs): """Alternate constructor to create a ``GeoSeries`` from a file. Can load a ``GeoSeries`` from a file from any format recognized by `fiona`. See http://fiona.readthedocs.io/en/latest/manual.html for details. Parameters ---------- filename : str File path or file handle to read from. Depending on which kwargs are included, the content of filename may vary. See http://fiona.readthedocs.io/en/latest/README.html#usage for usage details. kwargs : key-word arguments These arguments are passed to fiona.open, and can be used to access multi-layer data, data stored within archives (zip files), etc. """ from geopandas import GeoDataFrame df = GeoDataFrame.from_file(filename, **kwargs) return GeoSeries(df.geometry, crs=df.crs)
def addNeighborhoods(data, utm10n): hoods = GeoDataFrame.from_file(os.getcwd()+'/diysco2-db/_main_/yvr-open-data-neighborhoods/csg_neighborhood_areas.shp'); print hoods.crs hoods.crs = utm10n output = data.copy() output.iscopy = False print len(output) output = sjoin(output, hoods, how="left") output['temp'] = [str(i.bounds) for i in output.geometry] print output['temp'].head() output = output.drop_duplicates('temp', keep="last") print len(output) # output.index = [i for i in range(len(otu))] for i in range(len(output)): if output['NAME'].iloc[i] is None: output['NAME'].iloc[i] = "Stanley Park" if output["MAPID"].iloc[i] is None: output['MAPID'].iloc[i] = "SP1" # output = output[pd.isnull(output.co2_avg_e] print len(output) return output
def calcgeostats(gridjoin, experiment, gridsize): # read in merged gridded geodata # join_inner_df = GeoDataFrame.from_file('/Users/Jozo/Dropbox/_Projects/_GitHub/MobileCO2/Projects/05_GroupTraverse_01/data/filtered_geojson_wgs84/output/gridjoin_250m.shp') # join_inner_df = GeoDataFrame.from_file(iofolder+'diysco2-grid/'+ 'gridjoin_'+str(gridsize)+'m'+'.shp') join_inner_df = gridjoin # read in grid # path2grid = "/Users/Jozo/Dropbox/_Projects/_GitHub/MobileCO2/Projects/05_GroupTraverse_01/data/grid/grid_250m_transect.shp" # path2grid = iofolder+'diysco2-grid/'+"grid_"+str(gridsize)+'m'+"_transect.shp" path2grid = os.getcwd() + '/diysco2-db/campaigns/'+experiment+'/diysco2-grid/'+'grid_'+str(gridsize)+'m'+"_transect.shp" grid = GeoDataFrame.from_file(path2grid) # create grid id to groupby grid['id'] = [i for i in range(len(grid))] # run describestats function co2_stats = describestats(join_inner_df, 'co2') temp_stats = describestats(join_inner_df, 'tempout') # Merge data together summarystats = pd.merge(co2_stats, temp_stats, left_index=True, right_index=True) # merge data to geogrid output = GeoDataFrame.merge(grid, summarystats, left_on="id", right_index=True) output = output.reset_index() # output = output.fillna(-9999) opath = os.getcwd() + '/diysco2-db/campaigns/'+experiment+'/diysco2-grid/' if(os.path.isdir(opath)): print "already a folder!" else: os.mkdir(opath) # output.to_file('/Users/Jozo/Dropbox/_Projects/_GitHub/MobileCO2/Projects/05_GroupTraverse_01/data/filtered_geojson_wgs84/output/gridstats_250m.shp') # output.to_file(opath+'/'+'gridstats_'+str(gridsize)+'m'+'.shp') output.to_file(os.getcwd() + '/diysco2-db/campaigns/'+experiment+'/diysco2-grid/'+'gridstats_'+str(gridsize)+'m'+".shp") # output.to_file(opath+'/'+'gridstats_'+str(gridsize)+'m'+'.geojson', driver="GeoJSON") return output
def setUp(self): # Data from http://www.nyc.gov/html/dcp/download/bytes/nybb_13a.zip # saved as geopandas/examples/nybb_13a.zip. self.df = GeoDataFrame.from_file( '/nybb_13a/nybb.shp', vfs='zip://examples/nybb_13a.zip')
details. First we'll import a dataset containing each borough in New York City. We'll use the ``datasets`` module to handle this quickly. """ import numpy as np import matplotlib.pyplot as plt from shapely.geometry import Point from geopandas import GeoSeries, GeoDataFrame import geopandas as gpd np.random.seed(1) DPI = 100 path_nybb = gpd.datasets.get_path('nybb') boros = GeoDataFrame.from_file(path_nybb) boros = boros.set_index('BoroCode') boros ############################################################################## # Next, we'll plot the raw data ax = boros.plot() plt.xticks(rotation=90) plt.savefig('nyc.png', dpi=DPI, bbox_inches='tight') ############################################################################## # We can easily retrieve the convex hull of each shape. This corresponds to # the outer edge of the shapes. boros.geometry.convex_hull.plot() plt.xticks(rotation=90)