How to get shapefile geometry type in PyQGIS?
I'm writing a script that is dependent on knowing the geometry type of the loaded shapefile.
but I've looked in the pyqgis cookbook and API and can't figure out how to call it.
infact, I have trouble interpreting the API, so any light shed on that subject would be appreciated.
Thank you
python shapefile qgis
add a comment |
I'm writing a script that is dependent on knowing the geometry type of the loaded shapefile.
but I've looked in the pyqgis cookbook and API and can't figure out how to call it.
infact, I have trouble interpreting the API, so any light shed on that subject would be appreciated.
Thank you
python shapefile qgis
add a comment |
I'm writing a script that is dependent on knowing the geometry type of the loaded shapefile.
but I've looked in the pyqgis cookbook and API and can't figure out how to call it.
infact, I have trouble interpreting the API, so any light shed on that subject would be appreciated.
Thank you
python shapefile qgis
I'm writing a script that is dependent on knowing the geometry type of the loaded shapefile.
but I've looked in the pyqgis cookbook and API and can't figure out how to call it.
infact, I have trouble interpreting the API, so any light shed on that subject would be appreciated.
Thank you
python shapefile qgis
python shapefile qgis
edited Nov 15 '18 at 10:44
Matt Needle
1055
1055
asked Aug 12 '14 at 21:03
Steven LutzSteven Lutz
155313
155313
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The command is simple:
layer=qgis.utils.iface.mapCanvas().currentLayer()
if layer.wkbType()==QGis.WKBPoint:
print 'Layer is a point layer'
if layer.wkbType()==QGis.WKBLineString:
print 'Layer is a line layer'
if layer.wkbType()==QGis.WKBPolygon:
print 'Layer is a polygon layer'
if layer.wkbType()==QGis.WKBMultiPolygon:
print 'Layer is a multi-polygon layer'
if layer.wkbType()==100:
print 'Layer is a data-only layer'
You can use numbers (1,2,3,4) instead of the QGis.WKB****
* syntax, but the way described above yields a more readable code.
The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
1
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
add a comment |
QgsGeometry has the method wkbType that returns what you want.
add a comment |
Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :
geomTypeString=qgis.core.QgsWKBTypes.displayString(int(layer.wkbType()))
that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.
For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)
geomFlatTypeString=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:
def copyLayer(in_layer,condition=None):
#condition=function to test features and return True or False______
if condition==None:
def condition(f):
return True
typeGeom=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
crsId=in_layer.crs().authid()
out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
in_layer.name()+"_copie",
"memory")
fields=in_layer.dataProvider().fields().toList()
out_layer.dataProvider().addAttributes(fields)
out_layer.updateFields()
features=[f for f in in_layer.getFeatures() if condition(f)]
out_layer.dataProvider().addFeatures(features)
return out_layer
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f25273987%2fhow-to-get-shapefile-geometry-type-in-pyqgis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The command is simple:
layer=qgis.utils.iface.mapCanvas().currentLayer()
if layer.wkbType()==QGis.WKBPoint:
print 'Layer is a point layer'
if layer.wkbType()==QGis.WKBLineString:
print 'Layer is a line layer'
if layer.wkbType()==QGis.WKBPolygon:
print 'Layer is a polygon layer'
if layer.wkbType()==QGis.WKBMultiPolygon:
print 'Layer is a multi-polygon layer'
if layer.wkbType()==100:
print 'Layer is a data-only layer'
You can use numbers (1,2,3,4) instead of the QGis.WKB****
* syntax, but the way described above yields a more readable code.
The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
1
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
add a comment |
The command is simple:
layer=qgis.utils.iface.mapCanvas().currentLayer()
if layer.wkbType()==QGis.WKBPoint:
print 'Layer is a point layer'
if layer.wkbType()==QGis.WKBLineString:
print 'Layer is a line layer'
if layer.wkbType()==QGis.WKBPolygon:
print 'Layer is a polygon layer'
if layer.wkbType()==QGis.WKBMultiPolygon:
print 'Layer is a multi-polygon layer'
if layer.wkbType()==100:
print 'Layer is a data-only layer'
You can use numbers (1,2,3,4) instead of the QGis.WKB****
* syntax, but the way described above yields a more readable code.
The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
1
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
add a comment |
The command is simple:
layer=qgis.utils.iface.mapCanvas().currentLayer()
if layer.wkbType()==QGis.WKBPoint:
print 'Layer is a point layer'
if layer.wkbType()==QGis.WKBLineString:
print 'Layer is a line layer'
if layer.wkbType()==QGis.WKBPolygon:
print 'Layer is a polygon layer'
if layer.wkbType()==QGis.WKBMultiPolygon:
print 'Layer is a multi-polygon layer'
if layer.wkbType()==100:
print 'Layer is a data-only layer'
You can use numbers (1,2,3,4) instead of the QGis.WKB****
* syntax, but the way described above yields a more readable code.
The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html
The command is simple:
layer=qgis.utils.iface.mapCanvas().currentLayer()
if layer.wkbType()==QGis.WKBPoint:
print 'Layer is a point layer'
if layer.wkbType()==QGis.WKBLineString:
print 'Layer is a line layer'
if layer.wkbType()==QGis.WKBPolygon:
print 'Layer is a polygon layer'
if layer.wkbType()==QGis.WKBMultiPolygon:
print 'Layer is a multi-polygon layer'
if layer.wkbType()==100:
print 'Layer is a data-only layer'
You can use numbers (1,2,3,4) instead of the QGis.WKB****
* syntax, but the way described above yields a more readable code.
The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html
edited Nov 15 '18 at 11:14
Matt Needle
1055
1055
answered Aug 14 '14 at 17:29
PCamargoPCamargo
342316
342316
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
1
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
add a comment |
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
1
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?
– banbar
Jul 17 '17 at 11:57
1
1
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).
– PCamargo
Jul 17 '17 at 21:32
add a comment |
QgsGeometry has the method wkbType that returns what you want.
add a comment |
QgsGeometry has the method wkbType that returns what you want.
add a comment |
QgsGeometry has the method wkbType that returns what you want.
QgsGeometry has the method wkbType that returns what you want.
answered Apr 12 '15 at 2:47
lcoandradelcoandrade
112
112
add a comment |
add a comment |
Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :
geomTypeString=qgis.core.QgsWKBTypes.displayString(int(layer.wkbType()))
that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.
For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)
geomFlatTypeString=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:
def copyLayer(in_layer,condition=None):
#condition=function to test features and return True or False______
if condition==None:
def condition(f):
return True
typeGeom=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
crsId=in_layer.crs().authid()
out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
in_layer.name()+"_copie",
"memory")
fields=in_layer.dataProvider().fields().toList()
out_layer.dataProvider().addAttributes(fields)
out_layer.updateFields()
features=[f for f in in_layer.getFeatures() if condition(f)]
out_layer.dataProvider().addFeatures(features)
return out_layer
add a comment |
Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :
geomTypeString=qgis.core.QgsWKBTypes.displayString(int(layer.wkbType()))
that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.
For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)
geomFlatTypeString=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:
def copyLayer(in_layer,condition=None):
#condition=function to test features and return True or False______
if condition==None:
def condition(f):
return True
typeGeom=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
crsId=in_layer.crs().authid()
out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
in_layer.name()+"_copie",
"memory")
fields=in_layer.dataProvider().fields().toList()
out_layer.dataProvider().addAttributes(fields)
out_layer.updateFields()
features=[f for f in in_layer.getFeatures() if condition(f)]
out_layer.dataProvider().addFeatures(features)
return out_layer
add a comment |
Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :
geomTypeString=qgis.core.QgsWKBTypes.displayString(int(layer.wkbType()))
that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.
For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)
geomFlatTypeString=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:
def copyLayer(in_layer,condition=None):
#condition=function to test features and return True or False______
if condition==None:
def condition(f):
return True
typeGeom=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
crsId=in_layer.crs().authid()
out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
in_layer.name()+"_copie",
"memory")
fields=in_layer.dataProvider().fields().toList()
out_layer.dataProvider().addAttributes(fields)
out_layer.updateFields()
features=[f for f in in_layer.getFeatures() if condition(f)]
out_layer.dataProvider().addFeatures(features)
return out_layer
Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :
geomTypeString=qgis.core.QgsWKBTypes.displayString(int(layer.wkbType()))
that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.
For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)
geomFlatTypeString=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:
def copyLayer(in_layer,condition=None):
#condition=function to test features and return True or False______
if condition==None:
def condition(f):
return True
typeGeom=qgis.core.QgsWKBTypes.displayString(int(
qgis.core.QgsWKBTypes.flatType(int(in_layer.wkbType()))))
crsId=in_layer.crs().authid()
out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
in_layer.name()+"_copie",
"memory")
fields=in_layer.dataProvider().fields().toList()
out_layer.dataProvider().addAttributes(fields)
out_layer.updateFields()
features=[f for f in in_layer.getFeatures() if condition(f)]
out_layer.dataProvider().addFeatures(features)
return out_layer
answered Aug 22 '17 at 12:47
Gui3Gui3
288
288
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f25273987%2fhow-to-get-shapefile-geometry-type-in-pyqgis%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