37

Nowadays (QGIS v3.x) Get the layer reference: layer = iface.activeLayer() Select features by expression: layer.selectByExpression( "\"ogc_fid\"=482" ) Before QGIS 2.16 Follow these steps: Get the layer reference: cLayer = iface.mapCanvas().currentLayer() Get a featureIterator from an expression: expr = QgsExpression( "\"ogc_fid\"=482" ) it = cLayer....


35

You miss a minus sign before where and the select is not necessary, so it should be: ogr2ogr -where ID="1" outfile.shp infile.shp or if you have to do more complex query on your input data: ogr2ogr -sql "SELECT * FROM infile WHERE ID='1'" outfile.shp infile.shp If ID is a field of Integer type, substitute ID='1' with ID=1. Notes: -f "ESRI Shapefile" is ...


26

You can also use the SQL IN operator "City Name" IN ('ROME','NAPLES')


24

You can achieve this with a very simple model if you have ArcGIS 10.0 or higher. Create a model with Feature Iterator where the group by field is the attribute you wish to select by then send the output to the copy features tool using inline substitution to ensure a unique file name. The model is shown below:


21

A more elegant solution is: "Classes" in ('14','15',....)


21

You can loop over the iterator and get the .id() for every feature in it: with edit(layer): # build a request to filter the features based on an attribute request = QgsFeatureRequest().setFilterExpression('"DN" != 3') # we don't need attributes or geometry, skip them to minimize overhead. # these lines are not strictly required but improve ...


17

A new feature just came up in QGIS 2.2 called "Paste as new vector layer": It is a common activity in a GIS to create a sub-selection and then to create a new layer from the selection. In QGIS you can already do save selection as to save a layer from your selection, and now offers functionality that allows you to create a new file or memory layer ...


17

Using the Select by Attributes tool, you could enter the following SQL query to select all features with less than 5 characters in the ZIP field: CHAR_LENGTH("ZIP") < 5


17

Very nice use case for yet another handy QGIS feature: Layer actions! Here the steps to configure your layer action: Open your layer Properties dialog. Click on menu Actions. Click on the green cross to add an action. Fill the dialog inputs as indicated in the following picture. You can Copy & Paste this couple of lines of code into the 'Action Text' ...


15

I think you need to use the following instead: "GRIDCODE" >= 10 AND "GRIDCODE" < 15 You need to switch the => operators and specify again the attribute field for the second term (i.e. < 15).


13

You would need to break up your selection statement into two separate parts: "Classes" = '14' OR "Classes" = '07' Edit: Changed from AND to OR, since as noted in the comments, AND cannot work in this situation.


12

How about "ONScode" != '17UB' != means "not equal to".


11

In QGIS 2.6.2 Brighton, you can do the following: Select features Right click on the layer Save as... Select the option "Save only selected objects" Save That should do


11

The number values in your example query have quotation marks around them, so it looks like the problem is that your house numbers are stored as strings and not as numbers. You can check by right-clicking the field heading and clicking Properties. If the type is String, you'll need to create a new House Number field of the Integer type. Use the Field ...


11

There are two possibilities when you want to select several value. The first is to use the OR operator : "Parametro" = 'alfa-Hexaclorociclohexano' OR "Parametro" = 'beta-Hexaclorociclohexano' OR "Parametro" = 'delta-Hexaclorociclohexano' OR "Parametro" = 'gamma-Hexaclorociclohexano' You need to repeat the field name each time (this also work if you want ...


11

Use this expression: ("ALTURA_SOB" >=500 AND "ALTURA_SOB" <= 1200) OR ("ALTURA_SOB" >=2000 AND "ALTURA_SOB" <= 2500)


10

Follow these 5 steps in the QGIS Python console: Get the layer reference (I assume the layer is at the top of the ToC): l = iface.mapCanvas().layers()[0] Get a featureIterator from an expression: expr = QgsExpression( "\"EXCHANGE_1141_CODE\"='IH'" ) it = l.getFeatures( QgsFeatureRequest( expr ) ) Build a list of feature Ids from the result obtained in 2....


10

You would be better off using "Select by expression" instead of "Select by attribute". Your expression should be something like: "AREA" = maximum("AREA") Replace "AREA" in the expression with the name of the column. Alternatives are: $area = maximum($area) or area($geometry) = maximum(area($geometry)) For ...


10

QDateTimeobject is returned from maximum, but inspection_date is probably QDate. And to_date converts the result to QDate object. "inspection_date" = to_date(maximum("inspection_date", group_by:="box_id"))


9

Here is another query option. It largely does the same as the answers listed above but is (in my opinion) a little bit easier to customise for different scenarios. To display any contour divisible by 10 Floor(Elevation/10)=Elevation/10 To display any contour divisible by 50 Floor(Elevation/50)=Elevation/50 To display half metre contours Floor(...


9

I used @AlexandreNeto's script and updated it for ArcGIS 10.x users. Mainly you now have to import "arcpy" instead of "arcgisscripting": # Script created to separate one shapefile in multiple ones by one specific # attribute # Example for a Inputfile called "my_shapefile" and a field called "my_attribute" import arcpy #Set Input Output variables inputFile ...


9

It is unclear to me what you want to do exactly. The expression you are using is correct. It highlights the points with the correct expression because you are using "Select By Expression" and the function works exactly as it should be. If you want to show only those points and hide other points that do not comply with the rule, then you need to use ...


9

You can create a new function which returns a percentile value, then, compare the field value. Open Select Feature by Expression tool Create new function in the Function Editor using the script below. (How to use the Function Editor) from qgis.core import * from qgis.gui import * import numpy as np values, layer = None, None @qgsfunction(args='auto', ...


8

You can also use the column filter of the attribute table. It works default as a 'like' expression without the % wild card. Result when typing a:


8

[Posted earlier as an "edit" to my own question. Per Sorin's advice, I'm changing this to an answer instead of an edit.] I think I may have solved it. I used a percent sign ( % ) as a "wild card" along with the LIKE operator, and it looks like I got the expected search results. Example 1: In a roads shapefile layer, "FULLNAME" LIKE '%68%' produced a ...


8

The information you are looking for is hidden in a panel that is collapsed/minimized on the right-hand side of the dialog. Here's how you can access it:


8

You are doing fine, but just need to go to next step. Tick on Save only selected features.


8

Yes, you can use "select by expression" tool and insert if(is_selected() is false,true,false) as expression. This will allow inverting your selection in graphical modeler. Result:


7

This is an even easier way to do this... and it outputs into a GDB. http://www.umesc.usgs.gov/management/dss/split_by_attribute_tool.html download the tool from USGS, took me 3 minutes to do what i had been trying for 1 hour.


7

Alternatively to categorized styling, if you want to play a bit with colour you can try to code it by yourself: CASE WHEN Field = 'Value1' THEN color_rgb(0, 176, 80) WHEN Field = 'Value2' THEN color_rgb(255, 255, 0) WHEN Field = 'Value3' THEN color_rgb(255, 192, 0) WHEN Field = 'Value4' THEN color_rgb(255, 0, 0) WHEN Field = 'Value5' ...


Only top voted, non community-wiki answers of a minimum length are eligible