Aligning Raster and vector layer using PyQGIS when they do not match? The 2019 Stack Overflow...
How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?
Windows 10: How to Lock (not sleep) laptop on lid close?
Can we generate random numbers using irrational numbers like π and e?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
Drawing vertical/oblique lines in Metrical tree (tikz-qtree, tipa)
What aspect of planet earth must be changed to prevent the industrial revolution?
"is" operation returns false even though two objects have same id
What force causes entropy to increase?
Do I have Disadvantage attacking with an off-hand weapon?
Nested ellipses in tikzpicture: Chomsky hierarchy
Working through Single Responsibility Principle in Python when Calls are Expensive
How do you keep chess fun when your opponent constantly beats you?
Why can't wing-mounted spoilers be used to steepen approaches?
Why can I use a list index as an indexing variable in a for loop?
Can a flute soloist sit?
Why can't devices on different VLANs, but on the same subnet, communicate?
Pretty sure I'm over complicating my loops but unsure how to simplify
Didn't get enough time to take a Coding Test - what to do now?
The following signatures were invalid: EXPKEYSIG 1397BC53640DB551
Can the DM override racial traits?
How to substitute curly brackets with round brackets in a grid of list
Why doesn't a hydraulic lever violate conservation of energy?
Is 'stolen' appropriate word?
Aligning Raster and vector layer using PyQGIS when they do not match?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)PyQGIS raster image shifted from vector shapefileCreating memory layer with .png image using PyQGIS?Generate centroids for vector polygons based on raster and land-use data using QGISPlanar CRS for raster and vector layersHow to edit a vector layer extent?Raster and vector layer in the same CRS don't matchMerging raster and vector not workingOpening shapefile and Tiff layer Coordinates do not match upAlign raster to fishnet using ArcGIS 10.3interpolate raster values to the extent of a bigger vector layer in QGIS 2.18.16Is raster lossy and vector not lossy?Clip bathymetry raster file from vector mask layer its not working in QGIS 3.4.3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Relatively new to GIS. I've created a vector layer of points based on row/col position of high population density pixels in a raster layer. The layers do not align even though they have the same CRS, presumably because they do not have the same extent.
Is it possible to edit the extent of the raster to match that of the vector layer or when creating the new vector layer can I define extent based on my raster layer?

raster coordinate-system pyqgis vector
add a comment |
Relatively new to GIS. I've created a vector layer of points based on row/col position of high population density pixels in a raster layer. The layers do not align even though they have the same CRS, presumably because they do not have the same extent.
Is it possible to edit the extent of the raster to match that of the vector layer or when creating the new vector layer can I define extent based on my raster layer?

raster coordinate-system pyqgis vector
If my answer solved your issue you should mark it as accepted.
– xunilk
Apr 28 '17 at 21:17
add a comment |
Relatively new to GIS. I've created a vector layer of points based on row/col position of high population density pixels in a raster layer. The layers do not align even though they have the same CRS, presumably because they do not have the same extent.
Is it possible to edit the extent of the raster to match that of the vector layer or when creating the new vector layer can I define extent based on my raster layer?

raster coordinate-system pyqgis vector
Relatively new to GIS. I've created a vector layer of points based on row/col position of high population density pixels in a raster layer. The layers do not align even though they have the same CRS, presumably because they do not have the same extent.
Is it possible to edit the extent of the raster to match that of the vector layer or when creating the new vector layer can I define extent based on my raster layer?

raster coordinate-system pyqgis vector
raster coordinate-system pyqgis vector
edited 13 mins ago
PolyGeo♦
53.9k1782246
53.9k1782246
asked Apr 13 '17 at 20:22
SimonSimon
343
343
If my answer solved your issue you should mark it as accepted.
– xunilk
Apr 28 '17 at 21:17
add a comment |
If my answer solved your issue you should mark it as accepted.
– xunilk
Apr 28 '17 at 21:17
If my answer solved your issue you should mark it as accepted.
– xunilk
Apr 28 '17 at 21:17
If my answer solved your issue you should mark it as accepted.
– xunilk
Apr 28 '17 at 21:17
add a comment |
1 Answer
1
active
oldest
votes
To do that you need to get simultaneously x,y coordinates of each point by using some raster parameters (as xmin, ymax, and x, y resolution). Next code uses these parameters for obtaining a point memory layer where each point is situated at the middle of each raster cell.
layer = iface.activeLayer()
provider = layer.dataProvider()
extent = layer.extent()
xmin,ymin,xmax,ymax = extent.toRectF().getCoords()
rows = layer.height()
cols = layer.width()
xSize = layer.rasterUnitsPerPixelX()
ySize = layer.rasterUnitsPerPixelY()
block = provider.block(1, extent, cols, rows)
xinit = xmin + xSize/2
yinit = ymax - ySize/2
x = xinit
y = yinit
points = []
values = []
for i in range(rows):
for j in range(cols):
value = block.value(i,j)
if value > 150 and value < 255:
points.append(QgsPoint(x,y))
values.append(value)
x += xSize
y -= ySize
x = xinit
epsg = layer.crs().postgisSrid()
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer&field=value:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'point',
'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(points)) ]
for i, feat in enumerate(feats):
feat.setAttributes([i, values[i]])
feat.setGeometry(QgsGeometry.fromPoint(points[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
I used this population raster; where the criterion to get points was they represent values greater than 150 (254 = 10,000 persons/km2 and 255 = No data). After running the code at Python Console of QGIS I got:

Points over raster match as expected.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f236824%2faligning-raster-and-vector-layer-using-pyqgis-when-they-do-not-match%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To do that you need to get simultaneously x,y coordinates of each point by using some raster parameters (as xmin, ymax, and x, y resolution). Next code uses these parameters for obtaining a point memory layer where each point is situated at the middle of each raster cell.
layer = iface.activeLayer()
provider = layer.dataProvider()
extent = layer.extent()
xmin,ymin,xmax,ymax = extent.toRectF().getCoords()
rows = layer.height()
cols = layer.width()
xSize = layer.rasterUnitsPerPixelX()
ySize = layer.rasterUnitsPerPixelY()
block = provider.block(1, extent, cols, rows)
xinit = xmin + xSize/2
yinit = ymax - ySize/2
x = xinit
y = yinit
points = []
values = []
for i in range(rows):
for j in range(cols):
value = block.value(i,j)
if value > 150 and value < 255:
points.append(QgsPoint(x,y))
values.append(value)
x += xSize
y -= ySize
x = xinit
epsg = layer.crs().postgisSrid()
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer&field=value:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'point',
'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(points)) ]
for i, feat in enumerate(feats):
feat.setAttributes([i, values[i]])
feat.setGeometry(QgsGeometry.fromPoint(points[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
I used this population raster; where the criterion to get points was they represent values greater than 150 (254 = 10,000 persons/km2 and 255 = No data). After running the code at Python Console of QGIS I got:

Points over raster match as expected.
add a comment |
To do that you need to get simultaneously x,y coordinates of each point by using some raster parameters (as xmin, ymax, and x, y resolution). Next code uses these parameters for obtaining a point memory layer where each point is situated at the middle of each raster cell.
layer = iface.activeLayer()
provider = layer.dataProvider()
extent = layer.extent()
xmin,ymin,xmax,ymax = extent.toRectF().getCoords()
rows = layer.height()
cols = layer.width()
xSize = layer.rasterUnitsPerPixelX()
ySize = layer.rasterUnitsPerPixelY()
block = provider.block(1, extent, cols, rows)
xinit = xmin + xSize/2
yinit = ymax - ySize/2
x = xinit
y = yinit
points = []
values = []
for i in range(rows):
for j in range(cols):
value = block.value(i,j)
if value > 150 and value < 255:
points.append(QgsPoint(x,y))
values.append(value)
x += xSize
y -= ySize
x = xinit
epsg = layer.crs().postgisSrid()
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer&field=value:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'point',
'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(points)) ]
for i, feat in enumerate(feats):
feat.setAttributes([i, values[i]])
feat.setGeometry(QgsGeometry.fromPoint(points[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
I used this population raster; where the criterion to get points was they represent values greater than 150 (254 = 10,000 persons/km2 and 255 = No data). After running the code at Python Console of QGIS I got:

Points over raster match as expected.
add a comment |
To do that you need to get simultaneously x,y coordinates of each point by using some raster parameters (as xmin, ymax, and x, y resolution). Next code uses these parameters for obtaining a point memory layer where each point is situated at the middle of each raster cell.
layer = iface.activeLayer()
provider = layer.dataProvider()
extent = layer.extent()
xmin,ymin,xmax,ymax = extent.toRectF().getCoords()
rows = layer.height()
cols = layer.width()
xSize = layer.rasterUnitsPerPixelX()
ySize = layer.rasterUnitsPerPixelY()
block = provider.block(1, extent, cols, rows)
xinit = xmin + xSize/2
yinit = ymax - ySize/2
x = xinit
y = yinit
points = []
values = []
for i in range(rows):
for j in range(cols):
value = block.value(i,j)
if value > 150 and value < 255:
points.append(QgsPoint(x,y))
values.append(value)
x += xSize
y -= ySize
x = xinit
epsg = layer.crs().postgisSrid()
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer&field=value:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'point',
'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(points)) ]
for i, feat in enumerate(feats):
feat.setAttributes([i, values[i]])
feat.setGeometry(QgsGeometry.fromPoint(points[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
I used this population raster; where the criterion to get points was they represent values greater than 150 (254 = 10,000 persons/km2 and 255 = No data). After running the code at Python Console of QGIS I got:

Points over raster match as expected.
To do that you need to get simultaneously x,y coordinates of each point by using some raster parameters (as xmin, ymax, and x, y resolution). Next code uses these parameters for obtaining a point memory layer where each point is situated at the middle of each raster cell.
layer = iface.activeLayer()
provider = layer.dataProvider()
extent = layer.extent()
xmin,ymin,xmax,ymax = extent.toRectF().getCoords()
rows = layer.height()
cols = layer.width()
xSize = layer.rasterUnitsPerPixelX()
ySize = layer.rasterUnitsPerPixelY()
block = provider.block(1, extent, cols, rows)
xinit = xmin + xSize/2
yinit = ymax - ySize/2
x = xinit
y = yinit
points = []
values = []
for i in range(rows):
for j in range(cols):
value = block.value(i,j)
if value > 150 and value < 255:
points.append(QgsPoint(x,y))
values.append(value)
x += xSize
y -= ySize
x = xinit
epsg = layer.crs().postgisSrid()
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer&field=value:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'point',
'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(points)) ]
for i, feat in enumerate(feats):
feat.setAttributes([i, values[i]])
feat.setGeometry(QgsGeometry.fromPoint(points[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
I used this population raster; where the criterion to get points was they represent values greater than 150 (254 = 10,000 persons/km2 and 255 = No data). After running the code at Python Console of QGIS I got:

Points over raster match as expected.
answered Apr 14 '17 at 11:52
xunilkxunilk
14.9k31942
14.9k31942
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f236824%2faligning-raster-and-vector-layer-using-pyqgis-when-they-do-not-match%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If my answer solved your issue you should mark it as accepted.
– xunilk
Apr 28 '17 at 21:17