Posts

Showing posts with the label Python

[Code snippet]: Planet API Image Download Url

from requests.auth import HTTPBasicAuth import os import requests item_id = "20161109_173041_0e0e" item_type = "PSScene3Band" os.environ['PLANET_API_KEY'] = '3b711bededf6485a0' #not a real id asset_type = "visual" item_url = 'https://api.planet.com/data/v1/item-types/{}/items/{}/assets'.format(item_type, item_id) # Request a new download URL result = requests.get(item_url, auth=HTTPBasicAuth(os.environ['PLANET_API_KEY'], '')) download_url = result.json()[asset_type]['location'] print(download_url)

[Code Snippet] Find Custom Role of users that are in ArcGIS portal - ArcGIS

from arcgis.gis import * self.portalInfo = GIS(self.portalUrl, self.userName, self.password) self.portalUsers = self.portalInfo.users.search('') users = self.portalUsers roleManager = arcgis.gis.RoleManager(self.portalInfo) roles = roleManager.all() for user in users: if hasattr(user,'roleId'): for role in roles: if(user.roleId == role.role_id): print(user.username,user.role,role.name)

Code snippet: Get DateTime from UTC timestamp in ArcGIS Online

  ArcGIS Online  stores all date values in  UTC.  A short snippet to convert UTC timestamp to DateTime using Python.       from datetime import datetime       utcTimeStamp = 1472218452855       date = datetime.fromtimestamp( utcTimeStamp / 1e3)              print( date ) output: 2016-08-26 09:34:12.855000

[Code snippet] Extract Feature Layer Fields from Map Document (Mxd) using ArcPy

This code snippet is for getting a list of fields in a feature layer from a map document in ArcPy 10.4. 1: #Import arcpy mapping library 2: import arcpy.mapping; 3: 4: # Map doc path 5: mxdPath =r"C:\MyMxd.mxd" 6: 7: #Open map document 8: document = arcpy.mapping.MapDocument(mxdPath) 9: 10: #Extract all dataframes inside a map document 11: dataFrameList = arcpy.mapping.ListDataFrames(document) 12: 13: #Loop through all DF 14: for dataFrame in dataFrameList: 15: #Extact all layers in a dataframe 16: layerList = arcpy.mapping.ListLayers(document, None, dataFrame) 17: 18: #Loop through all Layers 19: for layer in layerList: 20: print (dataFrame.name+"--"+layer.name +"--"+layer.dataSource) 21: 22: #Extract all fields in a layer 23: fieldList = arcpy.ListFields(layer.dataSource, None, None) 24: 25: #Loop through and print field properties ...

Upgrade or Install Python PIP in ArcGIS Desktop 10.4.1

ArcGIS  Desktop 10.4.1’s default pip, C:\Program Files (x86)\Python27\ArcGIS10.4\Scripts  failed to install the packages throwing following error – Fatal error in launcher: Unable to create process using '"C:\Python27\ArcGIS10.4 \python.exe" "C:\Python27\ArcGISx6410.4\Scripts\pip.exe"  The following steps to make the pip to work Step 1. Open command terminal as Administrator and see if pip works       Make sure              C:\Program Files (x86)\Python27\ArcGIS10.4\Lib;             C:\Program Files (x86)\Python27\ArcGIS10.4\Scripts;             C:\Program Files (x86)\Python27\ArcGIS10.4       are in path                             pip install <package_name> , no luck [ You can Jump directly to #4]       ...

Python code snippet : Merge multiple CSVs (Column Wise) into a large CSV file using ID

Following is the python code snippet to merge multiple CSVs into a large CSV (column wise) using CSV's unique ID field. All input CSV's must have same length. import csv import itertools outCombinedStatTxtName ="FormattedTxtResults.csv" tempCsvFiles = glob.glob(str(TempFolderPath)+'\*.csv') openedFileList = [open(fn, 'rb') for fn in tempCsvFiles] readers = [csv.reader(fn) for fn in openedFileList] result = open(outCombinedStatTxtName, 'wb') writer = csv.writer(result,delimiter=',') multipleIndices = False for row_chunks in itertools.izip(*readers): tempFormattedRow = list(itertools.chain.from_iterable(row_chunks)) fidIndices = [i for i, x in enumerate(tempFormattedRow) if x == "ID"] if(len(fidIndices) > 1): fidIndices.pop(0) redundantIndices = tuple(fidIndices) multipleIndices = True if(multipleIndices): tempFormattedRow = [ tempFo...

Tiler tools, an alternative scripts to Gdal2tiles.py for creating raster tiles from digital maps

Image
Other day I was working to generate the Google Map based raster tiles using Gdal2tiles for a large raster file, 30GB in size. Gdal2tiels complains about the following error, which I had no clue. Then fiddling with others tilers available I played with the Tilers-tools , python Scripts for raster tile sets from digital maps with GDAL dependency. After spending few days hit and trial, finally I am able to generate raster tiles compatible with Google Maps. Here I am sharing the steps that I follow to produce tiles using the Tiler tool. Hope it will help some of the OSGeo users. I have divided tile generation steps into two groups, dependency installation and data preparation with a presumption that you are already familiar with GDAL . A] DEPENDENCY INSTALLATION STEP 1. Install Python geospatial packages Install the dependencies as described- https://github.com/GitHubRGI/geopackage-python/wiki/Installing-dependencies-on-Windows For my project, I used 32 bit inst...

Easy 5 steps to get thumbnails out of Pdf using Python

1) Install Ghost script before python dependencies installation from: http://www.a-pdf.com/convert-to-pdf/gs.exe 2) Make yourself familier with Wand , a  ctypes -based simple  ImageMagick  binding for Python. 3) Install Wand - pip install wand 4) Use the following script.. #Install Ghost script before python package's installation #from: http://www.a-pdf.com/convert-to-pdf/gs.exe   from wand.image import Image from wand.display import display fileDirectory = "D:/files/" inFileName="myInputfile.pdf" outFileName="myOutputfile.png" imageFromPdf = Image(filename=fileDirectory+inFileName) pages = len(imageFromPdf.sequence) print(pages) image = Image( width=imageFromPdf.width, height=imageFromPdf.height * pages ) for i in range(pages): image.composite( imageFromPdf.sequence[i], top=imageFromPdf.height * i, left=0 ) image.format="png" image.save(filename=f...

Python Scripting for ArcGIS

Image
Python Scripting for ArcGIS is a new text from Esri Press by Paul A. Zandbergen (2013). It isn’t the first Python book for the geospatial community or even focused on ArcGIS, but it is the first that has the Esri logo on it. Much like other recent books on Geo/Python we have seen, it focuses on integrating an introduction to Python with the industry specific materials. As Frank mentioned when he highlighted the book in a previous podcast, this allows users to gain exposure to Python, but it doesn’t fall back on the (in my opinion) bad habit of most programming texts of spending half of the book on the language and concepts before even getting to the application in the specific area. There is a time and place for that approach in Python specific books. When you add another software library to a book, then use it from the get go.

pyKML - a Python library for generating and parsing KML

Image
pyKML   is an open source Python library for generating, parsing, and modifying   KML , the geo-spatial data language used by Google Earth, Google Maps and a number of other GIS platforms. pyKML helps working with large and complex KML documents by leveraging the use of basic programming constructs (looping, branching, etc.). In this regard pyKML is similar to   libkml , Google’s open source C++ library, but takes advantage of the highly readable syntax of the   Python   programming language and the processing capabilities of the popular   lxml   Python library. pyKML v0.1.0 documentation :  http://packages.python.org/pykml/ Source: Google Geo Developers Blog

GIS Programming for GIS Analyst using Python

Image
Penn state has a nice collection of ArcGIS & Python programming step by step guide for their GEOG 485 students, which is also available for non-students via Penn state website.  This course’s is main aim is to make student able to use the Python scripting language to automate GIS tasks in ArcMap, assemble ArcView geoprocessing tools into models to solve GIS problems, and run the tools from scripts to automate GIS tasks.  Although n o previous programming experience is assumed, the course's target is to make a student as a GIS programmer/analyst in ArcGIS and Python environment.  The course is basically divided into four parts:

Raster Misalignment with Base Data in ArcMap10

Image
In the early Friday morning of mid May, I got an email from one of my team member about raster misalignment problem in ArcGIS10. I also tried to overlay couples of previously working Tiff and Grid raster files in ArcMap9.3 and ArcMap10 . The ArcMap9.3 overlay raster files perfectly aligned as we all desired, but ArcMap10 did not. Unaligned Aligned From the ESRI website, I got to know that the issue of misalignment of Tiff in ArcMap10 is a bug in ArcGIS 10. The ESRI team announced two solutions to solve Tiff shift into wrong geographic locations:

Getting MODIS Image Automatically From FTP in Python

# This is a Python script that automatically downloads historical # MODIS data from the LP DAAC FTP site # This version should work for all of the tiled datasets # It is currently hard-coded to downloaded specific MODIS tiles for # the northern Great Plains & upper midwest # Initailly historical date for Data transfer must be set on "lpdacc.txt". import os, ftplib,sys,string # Login information for accessing the LP DAAC FTP site Hostname = "e4ftl01u.ecs.nasa.gov" Username = "anonymous" Password = "@anonymous" # Get user inputs # Base directory for the MODIS data # Basedir = input("Enter the LP DAAC directory containing the dataset you want to download:") Basedir="MOLT/MOD11A2.005" print "The LP DAAC directory containing the dataset you want to download" +str(Basedir) # Local directory for data storage #Hdfdir = input("Enter the local directory where you w...

Simple GUI Demo in Python

from Tkinter import* mainForm=Tk() lblWelcome = Label(mainForm, text="Welcome to SSEBB Model") lblWelcome.pack() lblDisplay=Listbox(mainForm) txtMyval=Entry(mainForm) txtMyval.focus_set() txtMyval.pack() btnQuit=Button(mainForm,text="Close",command=quit) btnQuit.pack(side="left",padx=10,pady=10) def ShowVal(): lblDisplay.insert(0,txtMyval.get()) btnStart=Button(mainForm,text="Show in list",command=ShowVal) btnStart.pack() lblDisplay.pack(padx=20,pady=10) mainForm.mainloop()