0

This was probably asked before but I am not very good with the terminology so cannot seem to find what I am looking for.

I have a large set of data that has the parameters; date, longitude, and latitude. I downloaded a shapefile with the cities of the country the coordinates belong to. For each date, I want to count points in polygon and color the map for each date, and make an animation of it. I have tried using time manager on one CSV that contains all the data, however, I cannot seem to turn the points into polygons when I use it. The only option I can think of is to use count points in polygon for each individual layer (one for each date) but there are hundreds of layers.

Is there any simpler way to do this?

3
  • 1
    Sorry, I dont really understand how your data looks and what you want to achieve. Adding a screenshot could help.
    – Babel
    Aug 20 '21 at 18:36
  • By the way: you want to use time manager plugin for the animation or the (newer) temporal control?
    – Babel
    Aug 20 '21 at 19:35
  • @Babel I learned how to use Time manager plugin but I am open to trying different ways. The way the data looks is, I have 3 columns in each csv file, 'date', 'lon', 'lat'; the 'date' column has the same date value for each row and csv is named after this specific date. I have made delimited text layers with each csv file, and want to do 'Count Points in Polygons' from Processing > Toolbox. Then I want to merge the resulting polygons of each csv and animate it, however cannot seem to keep the 'date' column, also I have way too many layers to do it manually. Thanks for your interest!!
    – clarcia
    Aug 21 '21 at 7:44
2

Writing your steps one by one:

  • Load a points layer for a single date
  • Load a polygon layer
  • Run "Count Points in Polygons" from Processing > Toolbox
  • Add a date to the newly created layer

Next:

  • Collect all the created layers and concatenate them into a single layer
  • Set the temporal properties to use the appropriate datetime column
  • Animate

Your problem is: running "Count Points in Polygons" for every date will take forever, and is extremely tedious! The typical solution to this is writing a (Python) script, which will do the repeated step for you.

This can be done with PyQGIS, but it's a bit of a hassle.

It's much easier to do this with e.g. geopandas:

import geopandas as gpd
import pandas as pd
import glob

# Collect all the filenames of the csv's
csv_paths = glob.glob("*.csv")
# Load them as DataFrames
dfs = [pd.read_csv(path) for path in csv_paths]
# Concatenate them into a single big dataframe
df = pd.concat(dfs)
# Turn the dataframe into a GeoDataFrame
gdf = gpd.GeoDataFrame(
    df["date"], geometry=gpd.points_from_xy(x=df["lon"], y=df["lat"])
)
# Read the polygons
polygons = gpd.read_file("polygons.shp")
# Do a spatial join: find out for every point in which polygon it's located
joined = gpd.sjoin(gdf, polygons, op="within")
# Now count how many points there are per polygon
counts = joined.groupby(["date", "index_right"]).count().reset_index("date")
counts = counts.rename(columns={"geometry": "count"})
# Attach the counts back to the polygons
out = polygons.join(counts)
# Write to an output file
out.to_file("counts-over-time.shp")
# This file can be loaded into QGIS and used to animate
4
  • I was not very articulate in my explanation yet you understood everything and this looks like exactly what I am looking for!! Can't wait to try it!!! (Much more familiar with python than QGIS interface!) Thank you!!!
    – clarcia
    Aug 21 '21 at 4:23
  • I have tried it, it was giving an error due to CRS mismatch so I added .set_crs(epsg=4326) to gdf and polygons on joined = gpd.sjoin(gdf, polygons, op="within") However, it gives an error at out = polygons.join(counts) raise ValueError(f"columns overlap but no suffix specified: {to_rename}") ValueError: columns overlap but no suffix specified: Index(['adm1_tr', 'adm1_en', 'adm1', 'Shape_Leng', 'Shape_Area', 'adm0_en', 'adm0_tr', 'adm0'], dtype='object') i alternated with .merge(), told me to try pd.concat and that didnt work either cause of gdf Any advice? Thanks!
    – clarcia
    Aug 21 '21 at 6:22
  • 1
    nevermind i added suffixes for join() and it worked!! Thank you so much!!!!!
    – clarcia
    Aug 21 '21 at 8:39
  • Glad you were able to figure it out :) Aug 21 '21 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.