Tuesday, 1 January 2019

How to update a text column of selected features in QGIS?


Let's say I have a bunch of postcodes. After making a selection and having the records highlighted in the attribute table, how to update all of them and give them the same category?



In ArcGIS, after having the features selected, you simply had to use field editor and say for example: name = asd.


In QGIS that doesn't work. How to do it in QGIS?



Answer




  1. make sure the layer is in edit mode (otherwise changes will not happen)

  2. Select features to change (however you like)

  3. then open attribute table

  4. select the attribute to change

  5. type in what you want it to be (anything you like) - if it is a string then enclose it in '

  6. hit the update selected button (be careful here as it is right next to the update all button)


  7. Save your changes and exit edit mode.


enter image description here


Removing overlapping part of polygons using QGIS?


Here is what I'd like to achieve in QGIS.


Is there a processing tool to remove the overlapping part of polygons?


Remove overlapping part of polygons


It is also very important that they do not get merged during the process and that they keep their original attributes since I need to work with them afterwards.




Answer



You can use the 'Check Geometries' plugin (Vector/Geometry Tools/Check Geometries) to remove overlapping areas.


enter image description here enter image description here enter image description here


Writing selected features to shapefile using OGR and Python?


I have two shapefiles that intersect: one containing a line, and the other containing polygons. I'm trying to use OGR and Python to create a buffer around the line and select polygons within it, then write those features to a shapefile.



##import modules
import ogr
import gdalconst
import sys
import os

##Define input files
power = (r'C:\Users\Megan\Desktop\powerlines\PowerLine.shp')
parcel = (r'C:\Users\Megan\Desktop\parcels\Parcels.shp')
outName = ("BufferedParcels.shp")


##Define buffer width
bufferWidth = 250

##Assign driver
driver = ogr.GetDriverByName('ESRI Shapefile')

##Open files
powerData = driver.Open(power, gdalconst.GA_ReadOnly)
parcelData = driver.Open(parcel, gdalconst.GA_ReadOnly)


##Verify files were opened correctly, exit if not
if powerData is None:
print('Could not open ' + power)
sys.exit(1)
if parcelData is None:
print('Could not open ' + parcel)
sys.exit(1)

inputDS = driver.Open(power)

if inputDS is None:
print('Could not open', power)
sys.exit(1)

##Get feature layer and geometry from powerlines
powerLayer = powerData.GetLayer(0)
powerFeature = powerLayer.GetNextFeature()
powerGeom = powerFeature.GetGeometryRef()

##Get feature layer and count from parcels

parcelLayer = parcelData.GetLayer(0)
parcelFeatureCo = parcelLayer.GetFeatureCount()

##Re-write output file if it already exists
inputLayer = inputDS.GetLayer()
if os.path.exists(outName):
os.remove(outName)

##Apply buffer to powerline geometry, give affordance that it worked
powerBuffer = powerGeom.Buffer(bufferWidth)

print('Created 250 ft buffer around',power)
##Attempt to get output driver, exit in case of failure
try:
outputDS = driver.CreateDataSource(outName)
except:
print('Could not create',outName)
sys.exit(1)

##Define new layer, exit in case of failure
newLayer = outputDS.CreateLayer('BufferedParcels',geom_type = ogr.wkbPolygon)

if newLayer is None:
print('Could not create layer for buffer in output DS')
sys.exit(1)
##Get new layer definition
newLayerDef = newLayer.GetLayerDefn()

##Give affordance that program is running
##Set feature ID
##Loop through features in parcelLayer
print ('Writing the following parcels to shapefile:')

featureID = 0
for i in parcelLayer:
##parcelFeatures = parcelLayer.GetFeature(i)
parcelGeom = i.GetGeometryRef()
bufferContains = powerBuffer.Contains(parcelGeom)

names = i.GetField('SITUSADDR')
area = i.GetField('AREA')
relevantVals = (names, area)
fmt = '%s %s'

##Determine if features are within buffer
##Print stats if 'True'
if bufferContains == True:
print (fmt % relevantVals)
try:
newFeature = ogr.Feature(newLayerDef)
newFeature.SetGeometry(poly)
newFeature.SetFID(featureID)
newLayer.CreateFeature(newFeature)
except:

##print('Error printing shapefile.')
newFeature.Destroy()
##parcelLayer.Destroy()
featureID += 1
i.Destroy()

##Give affordance that program worked
print('Wrote', featureID, 'parcels to shapefile.')

inputDS.Destroy()

outputDS.Destroy()

The code above successfully writes a list of the polygons I'm looking for and creates a shapefile. However, the file has an empty attribute table and doesn't show any geometry.


Can anyone see where I'm going wrong?




remote sensing - Landsat in ArcGIS



I need to learn how to download, import and compose Landsat 8 in ArcGIS for natural colors (preferably in a GeoTIFF format). I have tried on my own but it seems that I am doing something wrong...


Can anyone help me and tell me the exact steps please?




arcgis desktop - Removing unnecessary vertices?


I have over sampled polygons that I need to simplify, without changing the shape. basically, I want to reduce the number of vertices, by eliminating the ones that are useless and have the polygons represented with the significant vertices only. I used to do that with ET GeoWizards Plug-in, but I don't have a license anymore. My table has 500.000 features, water polygons.


Any ideas?




shapefile - Join line segments sharing a common attribute, using GDAL


I'm having trouble rendering a shapefile consisting of trail routes broken down into many short segments. So I'd like to combine them into a small number of line features, which share a NAME and touch each other.


Is this possible using GDAL? Failing that, are there other tools that don't require scripting? (I see this Python script could probably be adapted but I'd prefer a more general solution if available...)




Answer



It is probably possible with GDAL but not extremely easy. Everything that can be done with SQL in SpatiaLite can also be done with GDAL http://www.gdal.org/ogr_sql_sqlite.html and this query could be used as starting point:


ogrinfo -dialect sqlite test.shp -sql "select attr, st_union(geometry) from test group by attr"

However, the query creates one MultiLineString for each value of "attr" even if it has distinct parts and more complicated SQL would be required.


Instead of finding the SQL solution you may consider using OpenJUMP


This is what you have in the beginning


enter image description here


Find the "Dissolve" tool


enter image description here



Select field(s) that you want to use for dissolving. "Merge linestrings" combines connected lines which is good for you.


enter image description here


However, line "a" is still a two-part MultiLineString


enter image description here


Next you can select all MultiLineStrings from the whole layer with Edit - Selection - Select by Geometry Type [MultiLineString] and press the right button of the mouse for selecting "Explode Selected Features" and the result should please you.


enter image description here


The same can probably be done also with QGIS by starting with algorithm QGIS geoalgorithmns - Vector geometry tools - Dissolve but as I tried it I did not find a way for combining the connected "a" segments into a single linestring element.


wfs t - Failed to insert data to geoserver layer with WFS-T


I am trying to make a POST request to geoserver in order to Insert a new feature. I am using the example provided by the Demo section of geoserver.


I have modified it as following:


  xmlns:wfs="http://www.opengis.net/wfs"

xmlns:geonode="http://www.openplans.org/geonode"
xmlns:gml="http://www.opengis.net/gml">







88.247354197,21.438097723 89.247354197,18.438097723






alley





When trying to execute it I get a message:


 Feature type 'bgd_uti_transmissionline_nrel' is not available:

Any idea why this is happening?


EDITED I tried to make a DescribeFeature request as:


  version="1.0.0"
service="WFS"
xmlns="http://www.opengis.net/wfs"
xmlns:topp="http://www.openplans.org/topp"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://www.opengis.net/wfs">
geonode:bgd_uti_transmissionline_nrel


I did the request through the Demo section of geoserver. I get nothing back. Checking the Developer's tools, I see the request is pending.


EDIT II


I did the DescribeFeature request using PYTHON Requests and I get something meaningful:



























When I tried to do the InsertFeature request using PYTHON Requests, I got this error:




Feature type 'bgd_uti_transmissionline_nrel' is not available:



Answer



There were two issues in my requests. The first one was related with the URL. Initially I was using this URL:


url = 'http://localhost:8080/geoserver/wfs'


Including my_workspace name in the URL is necessary for this WFS-T Insert (according to here):


url = 'http://localhost:8080/geoserver/my_workspace/wfs'

The second issue was related with the namespace I have defined: geonode In the end the xml I used looked like this:


 xmlns:wfs="http://www.opengis.net/wfs"
xmlns:gml="http://www.opengis.net/gml">








39.247354197,21.438097723 89.247354197,18.438097723










The rest of the request command (using PYTHON Requests lib) is straightforward:


headers = {'Content-Type': 'application/xml'}

print requests.post(url, data=xmlstr, headers=headers, auth=('geoserver_user', 'geoserver_pass')).text

The problem to solve now is how to pass non spatial data in the request. Meaning values of the attributes.



arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...