Friday 14 February 2020

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 found in this post?


import_path = r"..."   # Path of .mxd
export_path = r"..." # Path of output file
field_name = "Name" # Name of field used to sort DDP


mxd = arcpy.mapping.MapDocument(import_path)
for i in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = i
row = mxd.dataDrivenPages.pageRow
print row.getValue(field_name)
arcpy.mapping.ExportToJPEG(mxd, export_path + "." + row.getValue(field_name) + ".jpg")
del mxd

Answer



If you define a new variable: pageName = mxd.dataDrivenPages.pageRow.Name You can then include the variable in the export path: str(pageName)


import_path = r"..."   # Path of .mxd

export_path = r"..." # Path of output file
field_name = "Name" # Name of field used to sort DDP

mxd = arcpy.mapping.MapDocument(import_path)
for i in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = i
row = mxd.dataDrivenPages.pageRow

pageName = mxd.dataDrivenPages.pageRow.Name


print row.getValue(field_name)
arcpy.mapping.ExportToJPEG(mxd, export_path + "." + row.getValue(field_name) + str(pageName) +".jpg")
del mxd

gdalwarp - jpg to geotiff transformation with gdal_translate and gcps fails


I am trying to create a geotiff from a jpeg by inputting gcps with gdal_translate:


gdal_translate -of GTiff -a_srs EPSG:4326 -gcp [1616.0 0 -121.459869532 38.5822533549] -gcp [0 0 -121.460081357 38.5831052365] -gcp [0 1080.0 -121.460807872 38.5829948432] -gcp [1616.0 1080.0 -121.460596039 38.582142963] pict20140910_131103_0.JPG tmp.tif


followed by gdalwarp to set the geotiff projection:


gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 tmp.tif geo_pict20140910_131103_0.tif

however the gdalwarp process fails with:



ERROR 1: Failed to compute GCP transform: Transform is not solvable.



The gcp coordinates are the image footprint vertices and were calculated using the image location, altitude, orientation and camera attributes. The gcps are in lat/lon (wgs84 epsg:4326), and the image size is 1616x1080.


Here is the gdalinfo for the file after gcps have been added, but before gdalwarp:


Driver: GTiff/GeoTIFF

Files: tmp.tif
Size is 1616, 1080
Coordinate System is `'
GCP Projection =
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0],

UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]]
GCP[ 0]: Id=1, Info=
(0,0) -> (-121.459869532,38.5822533549,0)
GCP[ 1]: Id=2, Info=
(0,0) -> (-121.460081357,38.5831052365,0)
GCP[ 2]: Id=3, Info=
(0,1079) -> (-121.460807872,38.5829948432,0)
GCP[ 3]: Id=4, Info=
(0,1079) -> (-121.460596039,38.582142963,0)

Metadata:
AREA_OR_POINT=Area
EXIF_BrightnessValue=(4.275)
EXIF_ColorSpace=1
EXIF_ComponentsConfiguration=0x1 0x2 0x3 00
EXIF_CompressedBitsPerPixel=(3)
EXIF_Contrast=0
EXIF_CustomRendered=0
EXIF_DateTime=2014:09:10 13:11:02
EXIF_DateTimeDigitized=2014:09:10 13:11:02

EXIF_DateTimeOriginal=2014:09:10 13:11:02
EXIF_DigitalZoomRatio=(1)
EXIF_ExifVersion=0230
EXIF_ExposureBiasValue=(0)
EXIF_ExposureMode=0
EXIF_ExposureProgram=4
EXIF_ExposureTime=(0.001)
EXIF_FileSource=0x3
EXIF_Flash=16
EXIF_FlashpixVersion=0100

EXIF_FNumber=(4)
EXIF_FocalLength=(16)
EXIF_FocalLengthIn35mmFilm=24
EXIF_GPSAltitude=(66)
EXIF_GPSAltitudeRef=00
EXIF_GPSImgDirection=(79)
EXIF_GPSImgDirectionRef=T
EXIF_GPSLatitude=(38) (34) (57.45)
EXIF_GPSLatitudeRef=N
EXIF_GPSLongitude=(121) (27) (37.22)

EXIF_GPSLongitudeRef=W
EXIF_GPSVersionID=0x2 0x3 00 00
EXIF_Interoperability_Index=R98
EXIF_Interoperability_Version=0x30 0x31 0x30 0x30
EXIF_ISOSpeedRatings=2000
EXIF_LightSource=0
EXIF_Make=SONY
EXIF_MaxApertureValue=(2.96875)
EXIF_MeteringMode=5
EXIF_Model=NEX-5T

EXIF_Orientation=1
EXIF_PixelXDimension=1616
EXIF_PixelYDimension=1080
EXIF_ResolutionUnit=2
EXIF_Saturation=0
EXIF_SceneCaptureType=0
EXIF_SceneType=0x1
EXIF_Sharpness=0
EXIF_Software=NEX-5T v1.01
EXIF_WhiteBalance=0

EXIF_XResolution=(350)
EXIF_YCbCrPositioning=2
EXIF_YResolution=(350)
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 1080.0)
Upper Right ( 1616.0, 0.0)
Lower Right ( 1616.0, 1080.0)

Center ( 808.0, 540.0)
Band 1 Block=1616x1 Type=Byte, ColorInterp=Red
Band 2 Block=1616x1 Type=Byte, ColorInterp=Green
Band 3 Block=1616x1 Type=Byte, ColorInterp=Blue

Answer



The gdal_translate document page http://www.gdal.org/gdal_translate.html may indeed give an impression that the values for Ground Control Points should be closed between brackets



[-gcp pixel line easting northing [elevation]]*



However, that is not the case as close reading reveals and correct syntax for your case is



gdal_translate -of GTiff -a_srs EPSG:4326 -gcp 1616.0 0 -121.459869532 38.5822533549 -gcp 0 0 -121.460081357 38.5831052365 -gcp 0 1080.0 -121.460807872 38.5829948432 -gcp 1616.0 1080.0 -121.460596039 38.582142963 pict20140910_131103_0.JPG tmp.tif

That gdal_translate accepts the input with brackets without sending an error message and makes on invalid output as a result may be considered as a bug. Write a mail to gdal-dev mailing list and ask an opinion.


A hint for the future: The best format to use for the interim result is GDAL Virtual raster .VRT http://www.gdal.org/gdal_vrttut.html. It is a small text file that has only a reference to the physical image file. Because image data is not copied you will get the result faster and save disk space. In the next step you just warp the VRT file.


gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 tmp.vrt geo_pict20140910_131103_0.tif

data - Seeking shapefile for postal codes/zip codes of India?


I have been searching for free/paid (preferably free) sources for postal code shapefiles for India.


Does anyone know where I can find one?





How to prevent small polygon WMS features to be hidden when zoomed out (GeoServer + OpenLayers)


Currently I'm developing a Web-GIS app where I use GeoServer 2.61 and OpenLayers 2. The map data is served from PostgreSQL+PostGIS via GeoServer (WMS).


I'm having a problem when I'm viewing polygon map. Some maps have a very little polygon feature, which do not show at certain zoom level when I zoom out (because the feature will be too small). Is there any workaround to prevent this to happen? I need to see ALL the polygon features at the lowest zoom level.



Answer



Polygon layers by default do not stay the same size when zooming further out because they represent an area. The only aspect that you will see for smaller polygons when zoomed out is just the outline. If you still want to represent the feature when zoomed far out one option would be to add a within the sld that will turn on at x scale (defining MIN and MAX scales for the polygon and point symbolizers). Refer to this Q/A for how to set this up:


Use SLD to conditionally display points or polygons based on zoom level?



arcgis desktop - Setting single output location for multiple files in ModelBuilder?


I have created a model in ModelBuilder. The first step is for the user to specify the location of the File Geodatabase that all the outputs will be saved in. What I want is for all the outputs (from the different tools within the model) to be saved in that FGDB; however, it could be called anything and located anywhere.


So how do I cause the output to be saved in the location that is specified in the first step?



Answer



This page on Esri's site should give you all the information you need to do this within ModelBuilder. Essentially you create a variable for the output folder/geodatabase -- which can be user-generated or hardcoded -- and then call it in the other tools by its name, surrounded by % symbols.


arcmap - Inserting newlines in Rectangle Text elements via ArcPy causes overlap?


I ran across an issue the other day when I tried to use ArcPy's mapping module to edit rectangle text elements with newlines (\n) in an ArcMap document. Here's what the output looked like:


enter image description here


Here's the code I used to generate that output. The first column are rectangle text elements Text1, Text2, Text3 going down; the second column are "plain" text elements Text4, Text5, and Text6 going down.


import os
import arcpy

HomeDir = r"C:\Desktop"
arcpy.env.workspace = HomeDir


CurrentMXD = arcpy.mapping.MapDocument(r"C:\Desktop\TextTest.mxd")
OutputFilename = r"C:\Desktop\TextTest.pdf"
if os.path.exists(OutputFilename):
os.remove(OutputFilename)

for TextElement in arcpy.mapping.ListLayoutElements(CurrentMXD, "TEXT_ELEMENT"):
TextElementName = TextElement.name

String1 = "The quick brown fox jumped over the lazy dog.\nShe sells sea shells by the sea shore."
String2 = "The quick brown fox \njumped over the lazy dog.\nShe sells sea shells by the sea shore."

String3 = "The quick brown fox jumped \nover the lazy dog.\nShe sells sea shells by the sea shore."

if TextElementName == "Text1":
TextElement.text = String1
if TextElementName == "Text2":
TextElement.text = String2
if TextElementName == "Text3":
TextElement.text = String3
if TextElementName == "Text4":
TextElement.text = String1

if TextElementName == "Text5":
TextElement.text = String2
if TextElementName == "Text6":
TextElement.text = String3

arcpy.mapping.ExportToPDF(CurrentMXD, OutputFilename)

So far, it looks like the presence of the messed up text depends on whether the line is longer enough to wrap, and whether the line before the newline is longer than the line after the newline.


Any ideas about what could be going wrong? Is there a workaround? I could use plain text elements and worry about wrapping lines using Python, but I'm hoping I can figure something out.



Answer




I ran into this as well. It's because ArcGIS requires Windows line endings which are both a carriage return and a line feed. Bit of a pain. Fortunately it's easy to get around. In Python by instead of just \n (which is linefeed - see the Python docs for more if you're keen), use \r\n.


Thursday 13 February 2020

dem - Calculating cells elevation (height) above nearest stream cell using ArcGIS Spatial Analyst


I have searched for some ideas on how to formulate this analysis, but have come up short. I imagine there is a pretty straight forward answer; any help would be greatly appreciates.


Data: DEM, 1/9th arc second (~10-meter), 239,891,470 cells, projected; Streams, National Hydrology Data set, high resolutions, 9,470 features vector; Streams, Same as above, but converted to a raster


Problem: Need to create a raster of the same resolution and extent as the DEM where each cells's value is its height (in meters) above the nearest stream (line or cell).


I am working with ArcGIS 10.1 with spatial analyst. I would prefer to run this analysis in Python, but can do it in R if it is easier.


Edit to clarify: each cell in my stream raster has the value of the elevation at that point. All cells that are not within a stream have a value of zero. For every cell in the extent of the DEM, I am looking to find the nearest stream cell and subtract the value of that cell (the elevation) from the elevation of the cell being analyzed. Calculate the difference in elevation between each cell and the nearest cell representing a stream.



Answer



@whuber has it right when they commented:



If "nearest" means in terms of distance on the map, then just subtract the Euclidean allocation of the stream elevations from the DEM. The procedure is illustrated in an answer to a related question.




First, get the euclidean allocation. Second, use the minus operation to get the difference between the two.


Can be easily done in python.


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...