def distance(self, sources, destinations, x='<STR_LIT:x>', y='<STR_LIT:y>', isolation=True,<EOL>export_raster=False, export_shape=False, routes=False, path='<STR_LIT>'):
start_points = sources.copy()<EOL>end_points = destinations.copy()<EOL>if (not isinstance(start_points, pd.core.frame.DataFrame) and<EOL>not isinstance(start_points, gp.geodataframe.GeoDataFrame)):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if (not isinstance(end_points, pd.core.frame.DataFrame) and<EOL>not isinstance(end_points, gp.geodataframe.GeoDataFrame)):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not self.mcp_cost:<EOL><INDENT>self.mcp()<EOL><DEDENT>count = <NUM_LIT:0><EOL>start_points['<STR_LIT>'], start_points['<STR_LIT>'] = self.map_pixel_location(start_points[x],<EOL>start_points[y])<EOL>end_points['<STR_LIT>'], end_points['<STR_LIT>'] = self.map_pixel_location(end_points[x], end_points[y])<EOL>start_points['<STR_LIT>'] = start_points.index.values<EOL>end_points['<STR_LIT>'] = end_points.index.values+start_points['<STR_LIT>'].max()+<NUM_LIT:1><EOL>for i in start_points.iterrows():<EOL><INDENT>cumulative_costs, traceback = self.mcp_cost.find_costs([[i[<NUM_LIT:1>].row, i[<NUM_LIT:1>].col]])<EOL>dist = cumulative_costs[end_points.row.values, end_points.col.values].transpose()/(<NUM_LIT:7>*<NUM_LIT>)<EOL>df2 = pd.DataFrame(np.array([(i[<NUM_LIT:1>]['<STR_LIT>']*np.ones_like(dist)).flatten(),<EOL>end_points['<STR_LIT>'], dist.flatten()]).transpose(),<EOL>columns=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>df2 = df2.loc[df2['<STR_LIT>'] < np.inf]<EOL>if isolation:<EOL><INDENT>grisolation = np.ma.masked_array(cumulative_costs,<EOL>mask=np.logical_or(self.raster.mask, cumulative_costs == np.inf)<EOL>, fill_value=np.nan).mean()/(<NUM_LIT:7>*<NUM_LIT>)<EOL>start_points.loc[i[<NUM_LIT:0>], '<STR_LIT>'] = grisolation<EOL><DEDENT>if export_raster:<EOL><INDENT>cumulative_costs = GeoRaster(np.ma.masked_array(cumulative_costs,<EOL>mask=np.logical_or(self.raster.mask,<EOL>cumulative_costs == np.inf),<EOL>fill_value=np.nan), self.geot, self.nodata_value,<EOL>projection=self.projection, datatype=self.datatype)<EOL>cumulative_costs.raster.data[cumulative_costs.raster.mask] = cumulative_costs.nodata_value<EOL>cumulative_costs.to_tiff(path+str(i[<NUM_LIT:1>]['<STR_LIT>']))<EOL><DEDENT>if df2.size > <NUM_LIT:0>:<EOL><INDENT>if export_shape:<EOL><INDENT>routes = True<EOL><DEDENT>if routes:<EOL><INDENT>df2['<STR_LIT>'] = df2['<STR_LIT>'].apply(lambda x:<EOL>self.mcp_cost.traceback(end_points.loc[end_points['<STR_LIT>'] == x][['<STR_LIT>', '<STR_LIT>']].values[<NUM_LIT:0>]))<EOL>df2['<STR_LIT>'] = df2.geometry.apply(lambda x: [map_pixel_inv(y[<NUM_LIT:0>], y[<NUM_LIT:1>], self.geot[<NUM_LIT:1>],<EOL>self.geot[-<NUM_LIT:1>], self.geot[<NUM_LIT:0>], self.geot[-<NUM_LIT:3>]) for y in x])<EOL>df2['<STR_LIT>'] = df2.geometry.apply(lambda x: LineString(x) if int(len(x) > <NUM_LIT:1>)<EOL>else LineString([x[<NUM_LIT:0>], x[<NUM_LIT:0>]]))<EOL>df2 = gp.GeoDataFrame(df2, crs=cea)<EOL><DEDENT>if isolation:<EOL><INDENT>df2['<STR_LIT>'] = grisolation<EOL><DEDENT>if count == <NUM_LIT:0>:<EOL><INDENT>self.grdist = df2.copy()<EOL><DEDENT>else:<EOL><INDENT>self.grdist = self.grdist.append(df2)<EOL><DEDENT>count += <NUM_LIT:1><EOL><DEDENT><DEDENT>if routes:<EOL><INDENT>self.grdist = gp.GeoDataFrame(self.grdist, crs=cea)<EOL><DEDENT>if export_shape:<EOL><INDENT>start_pointscols = sources.columns.values<EOL>end_pointscols = destinations.columns.values<EOL>if '<STR_LIT>' in end_pointscols:<EOL><INDENT>self.grdist = pd.merge(self.grdist, end_points[['<STR_LIT>'] + end_pointscols.tolist()].drop('<STR_LIT>', axis=<NUM_LIT:1>), left_on='<STR_LIT>', right_on='<STR_LIT>', how='<STR_LIT:left>')<EOL><DEDENT>else:<EOL><INDENT>self.grdist = pd.merge(self.grdist, end_points[['<STR_LIT>']+end_pointscols.tolist()], left_on='<STR_LIT>', right_on='<STR_LIT>', how='<STR_LIT:left>')<EOL><DEDENT>if '<STR_LIT>' in self.start_pointscols:<EOL><INDENT>self.grdist = pd.merge(self.grdist, start_points[['<STR_LIT>']+start_pointscols.tolist()].drop('<STR_LIT>', axis=<NUM_LIT:1>), left_on='<STR_LIT>', right_on='<STR_LIT>', how='<STR_LIT:left>',<EOL>suffixes=['<STR_LIT>', '<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>self.grdist = pd.merge(self.grdist, start_points[['<STR_LIT>']+start_pointscols.tolist()], left_on='<STR_LIT>', right_on='<STR_LIT>', how='<STR_LIT:left>',<EOL>suffixes=['<STR_LIT>', '<STR_LIT>'])<EOL><DEDENT>self.grdist = gp.GeoDataFrame(self.grdist, crs=cea)<EOL>self.grdist.to_file(path+'<STR_LIT>')<EOL><DEDENT>
Compute cost distance measured from each start point to all end points.
The function returns the distances between the start point and the end
points as a Pandas dataframe. Additionally, for each start point it computes
the level of isolation, i.e. its average travel distance to all other locations