# imports import csv # inputs project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' # Step 1: manipulating original csv file # reading a csv-file with open(project_path + csv_file_name, mode='r', encoding="utf8") as csv_file: csv_reader = csv.reader(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) values = list(csv_reader) for row in values[1:]: row[16] = row[16].replace(",",".") row[18] = row[18].replace(",",".") # writing into the same csv-file while updated records are stored temporarily with open(project_path + csv_file_name, mode='w', encoding="utf8", newline= "") as csv_file: csv_writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in values: csv_writer.writerow(row) # Step 2: reading a csv file with points and converting it into a shapefile uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","latNieuw","longNieuw","epsg:4326") # In .format("UTF-8",",","x","y","epsg:31469") # "UTF-8" stands for data encoding (optional) # "," is a delimiter used in the input file # xField is a column name for longitude value # yField is a column name for latitude value # crs is a Coordinate system in EPSG number points = QgsVectorLayer(uri_csv_file, csv_file_name, "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) # Step 3: adding a shapefile with points into the QGIS's main window QgsProject.instance().addMapLayer(points) # imports import csv # inputs project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' # Step 1: manipulating original csv file # reading a csv-file with open(project_path + csv_file_name, mode='r', encoding="utf8") as csv_file: csv_reader = csv.reader(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) values = list(csv_reader) for row in values[1:]: row[16] = row[16].replace(",",".") row[18] = row[18].replace(",",".") # writing into the same csv-file while updated records are stored temporarily with open(project_path + csv_file_name, mode='w', encoding="utf8", newline= "") as csv_file: csv_writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in values: csv_writer.writerow(row) # Step 2: reading a csv file with points and converting it into a shapefile uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","latNieuw","longNieuw","epsg:4326") # In .format("UTF-8",",","x","y","epsg:31469") # "UTF-8" stands for data encoding (optional) # "," is a delimiter used in the input file # xField is a column name for longitude value # yField is a column name for latitude value # crs is a Coordinate system in EPSG number points = QgsVectorLayer(uri_csv_file, csv_file_name, "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) # Step 3: adding a shapefile with points into the QGIS's main window QgsProject.instance().addMapLayer(points) # imports import csv # inputs project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' # Step 1: manipulating original csv file # reading a csv-file with open(project_path + csv_file_name, mode='r', encoding="utf8") as csv_file: csv_reader = csv.reader(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) values = list(csv_reader) for row in values[1:]: row[16] = row[16].replace(",",".") row[18] = row[18].replace(",",".") # writing into the same csv-file while updated records are stored temporarily with open(project_path + csv_file_name, mode='w', encoding="utf8", newline= "") as csv_file: csv_writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in values: csv_writer.writerow(row) # Step 2: reading a csv file with points and converting it into a shapefile uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","latNieuw","longNieuw","epsg:4326") # In .format("UTF-8",",","x","y","epsg:31469") # "UTF-8" stands for data encoding (optional) # "," is a delimiter used in the input file # xField is a column name for longitude value # yField is a column name for latitude value # crs is a Coordinate system in EPSG number points = QgsVectorLayer(uri_csv_file, csv_file_name, "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) # Step 3: adding a shapefile with points into the QGIS's main window QgsProject.instance().addMapLayer(points) My suggestion is to changeIMHO the reason why a CSV file can not be added properly as a points layer file in QGIS are coordinates with commas instead of a decimal separator. All ',' have to be changed into '.' in coordinates, e.g.
ThenMoreover, as was mentioned by @MrXsquared, "longNieuw" and "latNieuw" have to be swapped.
Afterwards you will be able to add your csvCSV file into QGIS.
I did it withWith a bit of Python and some PyQGIS I was able to achieve the desired output
# imports import csv # inputs project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' # Step 1: manipulating original csv file # reading a csv-file with open(project_path + csv_file_name, mode='r', encoding="utf8") as csv_file: csv_reader = csv.reader(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) values = list(csv_reader) for row in values[1:]: row[16] = row[16].replace(",",".") row[18] = row[18].replace(",",".") # writing into the same csv-file while updated records are stored temporarily with open(project_path + csv_file_name, mode='w', encoding="utf8", newline= "") as csv_file: csv_writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in values: csv_writer.writerow(row) # Step 2: reading a csv file with points and converting it into a shapefile uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","longNieuw","latNieuw","longNieuw","epsg:4326") # In .format("UTF-8",",","x","y","epsg:31469") # "UTF-8" stands for data encoding (optional) # "," is a delimiter used in the input file # xField is a column name for longitude value # yField is a column name for latitude value # crs is a Coordinate system in EPSG number points = QgsVectorLayer(uri_csv_file, 'test'csv_file_name, "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) # Step 3: adding a shapefile with points into the QGIS's main window QgsProject.instance().addMapLayer(points)
Press Run script
and get the output that will look like
P.S.: I was able to modify only first two features
References:
My suggestion is to change ',' into '.' in coordinates, e.g.
Then you will be able to add your csv into QGIS.
I did it with PyQGIS
project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","longNieuw","latNieuw","epsg:4326") points = QgsVectorLayer(uri_csv_file, 'test', "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) QgsProject.instance().addMapLayer(points) P.S.: I was able to modify only first two features
IMHO the reason why a CSV file can not be added properly as a points layer file in QGIS are coordinates with commas instead of a decimal separator. All ',' have to be changed into '.', e.g.
Moreover, as was mentioned by @MrXsquared, "longNieuw" and "latNieuw" have to be swapped.
Afterwards you will be able to add your CSV file into QGIS.
With a bit of Python and some PyQGIS I was able to achieve the desired output
# imports import csv # inputs project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' # Step 1: manipulating original csv file # reading a csv-file with open(project_path + csv_file_name, mode='r', encoding="utf8") as csv_file: csv_reader = csv.reader(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) values = list(csv_reader) for row in values[1:]: row[16] = row[16].replace(",",".") row[18] = row[18].replace(",",".") # writing into the same csv-file while updated records are stored temporarily with open(project_path + csv_file_name, mode='w', encoding="utf8", newline= "") as csv_file: csv_writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in values: csv_writer.writerow(row) # Step 2: reading a csv file with points and converting it into a shapefile uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","latNieuw","longNieuw","epsg:4326") # In .format("UTF-8",",","x","y","epsg:31469") # "UTF-8" stands for data encoding (optional) # "," is a delimiter used in the input file # xField is a column name for longitude value # yField is a column name for latitude value # crs is a Coordinate system in EPSG number points = QgsVectorLayer(uri_csv_file, csv_file_name, "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) # Step 3: adding a shapefile with points into the QGIS's main window QgsProject.instance().addMapLayer(points) Press Run script
and get the output that will look like
References:
My suggestion is to change ',' into '.' in coordinates, e.g.
18,78416650000;-97,40638880 -> 18.78416650000;-97.40638880 Then you will be able to add your csv into QGIS.
I did it with PyQGIS
project_path = 'C:/Users/DUBRTARA/Desktop/test/project/' csv_file_name = 'DataNewTry.csv' uri_csv_file = 'file:///' + project_path + csv_file_name + "?encoding={0}&delimiter={1}&xField={2}&yField={3}&crs={4}".format("UTF-8",";","longNieuw","latNieuw","epsg:4326") points = QgsVectorLayer(uri_csv_file, 'test', "delimitedtext") if not points.isValid(): print ("{} layer was not loaded".format(csv_file_name)) QgsProject.instance().addMapLayer(points) P.S.: I was able to modify only first two features