domingo, 9 de outubro de 2022

Ponto no poligono

 import geopandas as gpd 

gpda = gpd.read_file('arquivo.json', driver='GeoJSON') 

import fiona

gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'

df = gpd.read_file('arquivo.kml', driver='KML')

dg=df['geometry']

env = gpda['geometry']

env.contains(dg)#The indices of the two GeoSeries are different.

Mapas de calor e outros

 import geopandas as gpd 

import matplotlib.pyplot as plt

gdf_geometrias_gsp = gpd.read_file('SP_Municipios_2020.shp') #arquivo de referencia

gdf_geometrias_gsp.head

shape2=['geometry'] #nome da coluna

data_2= shape[shape['NM_MUN']=='São Paulo'] #dados do arquiv de referencia

data_2.plot()

filename='dados_capital_sp.json'

print(data_2['geometry'])

data_2.to_file(filename, driver ="GeoJSON")

df_roubo=pd.read_excel('Celular.xlsx')

df_roubo.dropna(subset=['LATITUDE','LONGITUDE'])

df_roubo[['LATITUDE', 'LONGITUDE']]

df_roubo['geometry']=None

type(df_roubo)

for index, row in df_roubo.iterrows():

    df_roubo.loc[index,'geometry'] = Point(row.LONGITUDE, row.LATITUDE) #esse põem pontos

gdf_roubos= gpd.GeoDataFrame(df_roubo, geometry='geometry') 

gdf_roubos.plot()

gpd_capital_spp = gpd.read_file('dados_capital_sp.json', driver='GeoJSON')

import matplotlib.pyplot as plt 

polygon_sp= gpd_capital_spp.iloc[0].geometry

gdf_roubos_capital = gdf_roubos['geometry'].intersection(polygon_sp)

gdf_roubos_capital.plot()

gdf_roubos.plot()

gpd_capital_sp.plot()

import folium

fmap = folium.Map()

fig.ax = plt.subplots(figsize=(10,10))

gdf_roubos_capital.plot(ax=ax)

gpd_capital_sp.plot(ax=ax, facecolor='None', edgecolor='Black')

polygon_sp.plot(ax=ax, facecolor='None', edgecolor='Black')

import webbrowser

webbrowser.open("fmap.html")

gdf_roubos_capital.to_file('mapa1.json', driver ="GeoJSON") 

gdf_roubos.dropna(subset=["LATITUDE", "LONGITUDE"], inplace=True)

gdf_roubo_capital = gpd.read_file('mapa1.json', driver='GeoJSON')

gdf_roubos_capital_slice = gdf_roubo_capital.iloc[:100]# só 100 primeiros

gdf_roubos_slice = gdf_roubo.iloc[:100]

gdf_roubos_capital_slice.crs = "EPSG:29101"

medialat = gdf_roubos['LATITUDE'].mean() 

medialong = gdf_roubos['LONGITUDE'].mean()

fmap = folium.Map(location=[medialat, medialong])

feat_geojson=folium.features.GeoJson(gdf_roubos_capital_slice)

fmap.add_child(feat_geojson) #juntando os dados

fmap.save("fmap1.html")

webbrowser.open("fmap1.html")



from folium.plugins import FastMarkerCluster

fmap = folium.Map(location=[medialat, medialong])

mc=FastMarkerCluster=FastMarkerCluster(gdf_roubos[['LATITUDE','LONGITUDE']])

fmap.add_child(mc)

fmap.save("fmap2.html")

webbrowser.open("fmap2.html")

gdf_geometrias_gsp#limites dos municipios

limites=folium.features.GeoJson(gdf_geometrias_gsp, style_function=lambda feature:{'color': 'black', 'weight': '2', 'fillOpacity': '0.0'} )

fmap.add_child(limites)

from folium.plugins import HeatMap

fmap = folium.Map(location=[medialat, medialong], title ='cartodbpositron')

HeatMap = HeatMap (gdf_roubos[['LATITUDE','LONGITUDE']])

fmap.add_child(HeatMap)

fmap.save("fmap3.html")

webbrowser.open("fmap3.html")


for index, municipio in gdf_geometrias_gsp.iterrows():

    qtd_roubos = len(gdf_roubos[gdf_roubos.intersects(municipio.geometry)])

    gdf_geometrias_gsp.loc[index,'qtd_roubos']=qtd_roubos


media_latitude_gsp = gdf_roubos['LATITUDE'].mean()

media_longitude_gsp=gdf_roubos['LONGITUDE'].mean()

fmap = folium.Map(locations=[media_latitude_gsp, media_longitude_gsp],titles='cartodbpositron')



for _, municipios in gdf_geometrias_gsp.iterrows():

    municipio_geojson = folium.features.GeoJson(municipio.geometry, style_function = lambda feature:{'color':'blue', 'weinght': '2', 'fillOpacity':0.1})

    popup=folium.Popup("""Municipios:{} Roubo registrados em Novembro: {}""".format(municipio.NM_MUN, str(int(municipio.qtd_roubos)))) #'Series' object has no attribute

popup.add_to(municipio_geojson)

municipio_geojson.add_to(fmap)

fmap.save("fmap4.html")

webbrowser.open("fmap4.html")