This does not work any longer in qgis3. See a slightly modified answer below.
Have a look at the Memory provider as described in PyQGIS Cookbook.
Memory provider is intended to be used mainly by plugin or 3rd party app developers. It does not store data on disk, allowing developers to use it as a fast backend for some temporary layers.
# To avoid 'QVariant' is not defined error
from PyQt4.QtCore import *
# create layer
vl = QgsVectorLayer("Point", "temporary_points", "memory")
pr = vl.dataProvider()
# Enter editing mode
vl.startEditing()
# add fields
pr.addAttributes( [ QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double) ] )
# add a feature
fet = QgsFeature()
fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(10,10)) )
fet.setAttributeMap( { 0 : QVariant("Johny"),
1 : QVariant(20),
2 : QVariant(0.3) } )
pr.addFeatures( [ fet ] )
# Commit changes
vl.commitChanges()
I just used the below code to transfer a shapefile into PostGIS. I saw your post and thought it might help. The Shape@WKT makes it really easy to transfer the geometry. Everything is hard coded and works which I am happy with and will probably revisit at a later date when my coding improves. If anything is not clear please let me know.
import psycopg2, arcpy
# shapefile to get data from
data = ("C:/shapefiles/georgia_counties.shp")
# fields I want from shapefile
fields = ["AREANAME", "TotPop90", "PctRural", "PctBach", "PctEld", "PctFB", "PctPov", "PctBlack", "ID", "Shape@WKT"]
# pscopg2 connection, replace *** and *** with your values
connection = psycopg2.connect("dbname=*** user=***")
cursor = connection.cursor()
cursor.execute("DROP TABLE IF EXISTS georgia")
cursor.execute("""
CREATE TABLE georgia (
id SERIAL,
name VARCHAR,
totpop INTEGER,
pctrural DOUBLE PRECISION,
pctbach DOUBLE PRECISION,
pcteld DOUBLE PRECISION,
pctfb DOUBLE PRECISION,
pctpov DOUBLE PRECISION,
pctblack DOUBLE PRECISION,
county_id VARCHAR,
PRIMARY KEY (county_id))
""")
cursor.execute("""
SELECT AddGeometryColumn('georgia', 'geom', 32616, 'MULTIPOLYGON', 2)
""")
# use arcpy to get attribute data, populate PostGIS using psycopg2
with arcpy.da.SearchCursor(data, fields) as da_cursor:
for row in da_cursor:
wkt = row[9]
# the id was transferring as a float so this is just to remove decimal
id = int(row[8])
# this was tough - everything needs to be a string and text being inserted wrapped in '' including wkt
cursor.execute("INSERT INTO georgia (name, totpop, pctrural, pctbach, pcteld, pctfb, pctpov, pctblack, county_id, geom) VALUES (" + '\'' + row[0] + '\'' + ", " + str(row[1]) + ", " + str(row[2]) + ", " + str(row[3]) + ", " + str(row[4]) + ", " + str(row[5]) + ", " + str(row[6]) + ", " + str(row[7]) + ", " + str(id) + ", ST_GeometryFromText(" + "'" + wkt + "', 32616))")
connection.commit()
Best Answer
Ok, this is do able (If I understand correctly). Here is what I am thinking. Make 1 master shapefile with all the attributes. Create a csv with your list. Use python to read each line (species name) in the csv (use a for loop), use a copy function from ArcGIS or QGIS to copy the master shapefile and use the species name as the ouput name.
The following code uses Arcgis 10+
The following images are the various stages of the process.
Set-up of working Directory
Populating csv file
Script being run in the python shell
New shapefile based on the csv list
Hope this helps.