Skip navigation
All People > SWendel-esristaff > Stephanie's GIS Corner

Have you ever attached an image to your hosted feature service but then realized it's not rotated correctly? 

 

With the following script sample, you can rotate your image and re-upload it in one script!

 

Pre-requisites:

Install Python 2.7.x (Use what comes with your ArcGIS Desktop install  ) 

Install the Image Module: The Image Module 

Install the Requests Module: Requests: HTTP for Humans — Requests 2.18.4 documentation 

 

Both can be installed using pip. 

 

Code sample to adjust our attachments [See updated section below]: 

 

import requests
from PIL import Image
from StringIO import StringIO
import json

attachmentUrl = raw_input("URL including token to image that needs to be rotated: ")
rotateby = int(raw_input("Rotate Counterclockwise by: "))

featureURL = attachmentUrl.split("/attachments")[0]
serviceURL = attachmentUrl.rsplit("/", 3)[0]
attachmentID = ((attachmentUrl.split("/attachments")[1]).split('?token=')[0])[1:]
token = (attachmentUrl.split("/attachments")[1]).split('?token=')[1]
queryAttachmentsRequest = requests.get("{}/queryAttachments".format(serviceURL), params={'f':'json', 'token': token, 'objectIds': attachmentID})
name = json.loads(queryAttachmentsRequest.text)['attachmentGroups'][0]['attachmentInfos'][0]['name']
imgName = name.split('.')[0] + "_rotated.jpg"
imgTemp = r"C:/temp/" + imgName
attachmentRequest = requests.get(attachmentUrl)
img = Image.open(StringIO(attachmentRequest.content))
img2 = img.rotate(rotateby)
img2.save(imgTemp)
file = {'file': (imgName, open(imgTemp, 'rb'), 'image/jpg')}
addAttachmentURL = "{0}/addAttachment".format(featureURL)
params = {'f':'json','token':token}
addAttachmentRequest = requests.post(addAttachmentURL, files=file, data=params)
jsonresponse = addAttachmentRequest.text
print jsonresponse

 

The attachment URL can be gathered by opening the attachment through the map viewer. You should see the URL looks something like this:

http://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/Stephanie_Locations/FeatureServer/0/65/attachments/64?token=<OwnerToken>

 

Next you just need to provide the amount you want to rotate counterclockwise.

90 - Bottom right corner becomes top right corner.

180 - Bottom right corner becomes top left corner.

270 - Bottom right corner becomes bottom left corner.

 

I hope this gives you a starting place to start rotating image attachments! 

 

 

 

 

_______________________________________________________

Updated Section

 

Someone pointed out to me that the above code was actually doing the query for the attachments incorrectly because it was looking at a scenario where the feature's Object ID was the same as the Attachment ID. The code below is better suited for all scenarios especially when you have multiple attachments. 

 

import requests
from PIL import Image
from StringIO import StringIO
import json

attachmentUrl = raw_input("URL including token to image that needs to be rotated: ")
rotateby = int(raw_input("Rotate Counterclockwise by: "))

featureURL = attachmentUrl.split("/attachments")[0]
serviceURL = attachmentUrl.rsplit("/", 3)[0]
attachmentID = ((attachmentUrl.split("/attachments")[1]).split('?token=')[0])[1:]
attachment_feature_base = ((attachmentUrl.split("/attachments")[0]).split("/"))
attachment_feature_ID = attachment_feature_base[len(attachment_feature_base)-1]
print(attachment_feature_ID)
print(attachmentID)
token = (attachmentUrl.split("/attachments")[1]).split('?token=')[1]
queryAttachmentsRequest = requests.get("{}/queryAttachments".format(serviceURL), params={'f':'json', 'token': token, 'objectIds': attachment_feature_ID})
print(queryAttachmentsRequest.text)
attachments_group = json.loads(queryAttachmentsRequest.text)['attachmentGroups']
for attachment_list in attachments_group:
    attachment_info = attachment_list['attachmentInfos']
    for attachment in attachment_info:
        if attachment['id'] == int(attachmentID):
            name = attachment['name']
imgName = name.split('.')[0] + "_rotated.jpg"
imgTemp = r"C:/temp/" + imgName
attachmentRequest = requests.get(attachmentUrl)
img = Image.open(StringIO(attachmentRequest.content))
img2 = img.rotate(rotateby)
img2.save(imgTemp)
file = {'file': (imgName, open(imgTemp, 'rb'), 'image/jpg')}
addAttachmentURL = "{0}/addAttachment".format(featureURL)
params = {'f':'json','token':token}
addAttachmentRequest = requests.post(addAttachmentURL, files=file, data=params)
jsonresponse = addAttachmentRequest.text
print jsonresponse

Ever find yourself asking this question when joining a new parcel to your fabric? You've gone through all the points and are examining your parcel in the data view, but find yourself unsatisfied with the links you've created. Where do you start to make the corrections? You could go through the list and click each one with a high residual. How do you see it though in the data view? Clicking it will flash the link blue or you could click the zoom to button, but what if you were looking at the data view and saw a couple links that were way off? How do you go from the points to finding the join link?

 

It's actually really easy! Simply right click on the point and you're set! The join link will be selected in the join dialog. Here are all three options below. Happy joining!

 

JoinLinks.png